2

I want to generate a large number of key value pairs to put in my dictionary using a for loop. For example, the dictionary looks like this:

my_dict = dict()
my_dict["r0"] = "tag 0"
my_dict["r1"] = "tag 1"
my_dict["r2"] = "tag 2"
...

Note that both the key and value follows a pattern, i.e., the number increase by 1. Now I cannot do this 1M times and would prefer an automatic way to initialize my dictionary.

0

6 Answers 6

4

The most efficient way to do this is probably with a dict comprehension:

mydict={'r%s'%n : 'tag %s'%n for n in range(10)}

Which is equivalent to:

mydict=dict()
for n in range(10):
    mydict.update({'r%s'%n:'tag %s'%n})

... but more efficient. Just change range(10) as necessary.


You could also use .format() formatting instead of percent (C-like) formatting in the dict:

mydict={'r{}'.format(n) : 'tag {}'.format(n) for n in range(10)}

If you are using Python2 replace all the range() functions with xrange() functions

Sign up to request clarification or add additional context in comments.

Comments

1
my_dict = dict()
for i in range(0, 1000000):
    key = "r{}".format(i)
    value = "tag {}".format(i)
    my_dict[key] = value

EDIT: As pointed out by others, if you are using python 2 use xrange instead since it is lazy (so more efficient). In Python 3 range does the same thing as xrange in python 2

4 Comments

Question asks for 1 Million, not 1 thousand.
@KeithC, no the question did not explicitly say they needed this a million times, it was a figure of speach, not something to be taken literally... Regardless you can just change the 1000 to a million and this works...
changed the answer, same code still applies just with a few more 0s in the range
did this work for you? if so please mark answer as correct
0
my_dict = dict()
for i in xrange(1000000):
    my_dict["r%s" % i] = "tag %s" % i

Comments

0
my_dict = dict()
for x in range(1000000):
    key="r"+str(x)
    val="tag " +str(x)
    my_dict[key]=val

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!
0

simple way is to do the following

#using python format strings
keyf = "r{}"
valf = "tag {}"
#dictionary comprehension
a = {keyf.format(i) : valf.format(i) for i in range(5)}
# can modify range to handle 1,000,000 if you wanted
print(a)
{'r0': 'tag 0', 'r1': 'tag 1', 'r2': 'tag 2', 'r3': 'tag 3', 'r4': 'tag 4', 'r5': 'tag 5'}

if you wanted to quickly append this to another dictionary you would use the dictionary equivalent of extend, which is called update.

b = dict{"x":1,"y":2}
b.update(a)
print(b)
{'x': 1, 'y': 2, 'r0': 'tag 0', 'r1': 'tag 1', 'r2': 'tag 2', 'r3': 'tag 3', 'r4': 'tag 4'}

you could also shorten the original comprehension by doing this:

a = {"r{}".format(i) : "tag {}".format(i) for i in range(5)}

You wouldn't even need to make keyf, or valf

Comments

-2

Python can build dicts from lists:

$ python2 -c "print dict(map(lambda x: ('r' + str(x), 'tag ' + str(x)), range(10)))"
{'r4': 'tag 4', 'r5': 'tag 5', 'r6': 'tag 6', 'r7': 'tag 7', 'r0': 'tag 0', 'r1': 'tag 1', 'r2': 'tag 2', 'r3': 'tag 3', 'r8': 'tag 8', 'r9': 'tag 9'}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.