1

How would you create a dictionary in python, which would look a bit like this:

{1:0, 2:0, 3:0, 4:0, ...}

for a specific number of iterations?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    https://stackoverflow.com/questions/32544835/python-create-dictionary-using-dict-with-integer-keys[Check your Answer Through This Link][1] – eitzaz shah Dec 30 '20 at 18:42
  • Possible duplicate: [Creating a dictionary with same values](https://stackoverflow.com/questions/11977730/creating-a-dictionary-with-same-values) – wjandrea Dec 31 '20 at 04:24

3 Answers3

8

Use dict.fromkeys()

>>> dict.fromkeys(range(1, 5), 0)
{1: 0, 2: 0, 3: 0, 4: 0}
wjandrea
  • 28,235
  • 9
  • 60
  • 81
5
my_dict = {i:0 for i in range(1,x)}
3

You could do this:

dict(enumerate([0]*10,1))

{1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0}
Alain T.
  • 40,517
  • 4
  • 31
  • 51