How to create dictionary with dict() and range() in Python

1 Answer

0 votes
dic = [dict(num=-1 * i) for i in range(5)]

print(dic)



'''
run:

[{'num': 0}, {'num': -1}, {'num': -2}, {'num': -3}, {'num': -4}]

'''

 



answered Jun 1, 2019 by avibootz

Related questions

...