Categories

Dec 19, 2019 in Python
Q: What Is The Syntax For Dictionary Comprehension In Python?

1 Answer

Dec 19, 2019
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)
Click here to read more about Python
Click here to read more about Insurance

Related questions

Madanswer
Aug 30 in Python
Dec 19, 2019 in Python
Dec 14, 2019 in Python
...