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.

drdot
  • 3,215
  • 9
  • 46
  • 81

6 Answers6

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

Community
  • 1
  • 1
Juan T
  • 1,219
  • 1
  • 10
  • 21
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

Anis Jonischkeit
  • 914
  • 7
  • 15
0
my_dict = dict()
for i in xrange(1000000):
    my_dict["r%s" % i] = "tag %s" % i
Chris Curvey
  • 9,738
  • 10
  • 48
  • 70
0
my_dict = dict()
for x in range(1000000):
    key="r"+str(x)
    val="tag " +str(x)
    my_dict[key]=val
Kornelia
  • 43
  • 1
  • 3
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) 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! – Martin Tournoij May 04 '17 at 04:29
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

Krupip
  • 4,404
  • 2
  • 32
  • 54
-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'}
bipll
  • 11,747
  • 1
  • 18
  • 32