in Python by (31.6k points)
What Is The Syntax For Dictionary Comprehension In Python?

1 Answer

0 votes
by (23.9k points)
A dictionary has the same syntax as was for the list comprehension but the difference is that it uses curly braces:

{ aKey, itsValue for aKey in iterable }

For example, the below code will return all the numbers 10 to 20 as the keys and will store the respective squares of those numbers as the values.

>>> adict = {var:var**2 for var in range(10, 20)}

>>> print(adict)

Related questions

0 votes
asked Feb 11, 2021 in Python by SakshiSharma (32.2k points)