How to use dictionary comprehension to create a dictionary in Python

1 Answer

0 votes
dic = {x: x**2 for x in range(7)}
 
print(dic) 
 
 
 
'''
run:
 
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36}
 
'''

 



answered Dec 16, 2024 by avibootz
edited Dec 16, 2024 by avibootz

Related questions

...