0 votes
in Python by
What Is The Syntax For Dictionary Comprehension In Python?

1 Answer

0 votes
by
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)
...