There are dictionary comprehensions in Python 2.7+, but they don't work quite the way you're trying. Like a list comprehension, they create a new dictionary; you can't use them to add keys to an existing dictionary. Also, you have to specify the keys and values, although of course you can specify a dummy value if you like.

>>> d = {n: n**2 for n in range(5)}
>>> print d
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

If you want to set them all to True:

>>> d = {n: True for n in range(5)}
>>> print d
{0: True, 1: True, 2: True, 3: True, 4: True}

What you seem to be asking for is a way to set multiple keys at once on an existing dictionary. There's no direct shortcut for that. You can either loop like you already showed, or you could use a dictionary comprehension to create a new dict with the new values, and then do oldDict.update(newDict) to merge the new values into the old dict.

Answer from BrenBarn on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-dictionary-comprehension
Python Dictionary Comprehension - GeeksforGeeks
April 18, 2026 - Dictionary comprehension is used to create a dictionary in a short and clear way. It allows keys and values to be generated from a loop in one line.
Top answer
1 of 9
713

There are dictionary comprehensions in Python 2.7+, but they don't work quite the way you're trying. Like a list comprehension, they create a new dictionary; you can't use them to add keys to an existing dictionary. Also, you have to specify the keys and values, although of course you can specify a dummy value if you like.

>>> d = {n: n**2 for n in range(5)}
>>> print d
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

If you want to set them all to True:

>>> d = {n: True for n in range(5)}
>>> print d
{0: True, 1: True, 2: True, 3: True, 4: True}

What you seem to be asking for is a way to set multiple keys at once on an existing dictionary. There's no direct shortcut for that. You can either loop like you already showed, or you could use a dictionary comprehension to create a new dict with the new values, and then do oldDict.update(newDict) to merge the new values into the old dict.

2 of 9
175

You can use the dict.fromkeys class method ...

>>> dict.fromkeys(range(5), True)
{0: True, 1: True, 2: True, 3: True, 4: True}

This is the fastest way to create a dictionary where all the keys map to the same value.

But do not use this with mutable objects:

d = dict.fromkeys(range(5), [])
# {0: [], 1: [], 2: [], 3: [], 4: []}
d[1].append(2)
# {0: [2], 1: [2], 2: [2], 3: [2], 4: [2]} !!!

If you don't actually need to initialize all the keys, a defaultdict might be useful as well:

from collections import defaultdict
d = defaultdict(lambda: True)

To answer the second part, a dict-comprehension is just what you need:

{k: k for k in range(10)}

You probably shouldn't do this but you could also create a subclass of dict which works somewhat like a defaultdict if you override __missing__:

>>> class KeyDict(dict):
...    def __missing__(self, key):
...       #self[key] = key  # Maybe add this also?
...       return key
... 
>>> d = KeyDict()
>>> d[1]
1
>>> d[2]
2
>>> d[3]
3
>>> print(d)
{}
People also ask

What is the syntax for dictionary comprehension in Python?

The syntax for dictionary comprehension in Python is as follows: {key:value for (key,value) in iterable}

๐ŸŒ
datacamp.com
datacamp.com โ€บ home โ€บ tutorials โ€บ python โ€บ python dictionary comprehension: essentials you need to know
Python Dictionary Comprehension Tutorial | DataCamp
What are the benefits of using dictionary comprehension in Python?

The benefits of using dictionary comprehension in Python are that it is concise, readable, and reduces the amount of code needed to create dictionaries. It also allows for filtering and transformation of dictionary elements in a single line of code.

๐ŸŒ
datacamp.com
datacamp.com โ€บ home โ€บ tutorials โ€บ python โ€บ python dictionary comprehension: essentials you need to know
Python Dictionary Comprehension Tutorial | DataCamp
How do I create a dictionary from a list using dictionary comprehension in Python?

You can create a dictionary from a list using dictionary comprehension in Python by iterating over the list and setting the list elements as keys and values in the new dictionary.

๐ŸŒ
datacamp.com
datacamp.com โ€บ home โ€บ tutorials โ€บ python โ€บ python dictionary comprehension: essentials you need to know
Python Dictionary Comprehension Tutorial | DataCamp
๐ŸŒ
Python
peps.python.org โ€บ pep-0274
PEP 274 โ€“ Dict Comprehensions | peps.python.org
October 25, 2001 - You can use dict comprehensions in ways very similar to list comprehensions, except that they produce Python dictionary objects instead of list objects.
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ what-is-dictionary-comprehension-in-python
What Is Dictionary Comprehension in Python? | Codecademy
... Continue your Swift journey by learning these collection types: arrays, sets, and dictionaries! ... Dictionary comprehension is a feature in Python that allows us to build dictionaries in a single, elegant line of code.
๐ŸŒ
Switowski
switowski.com โ€บ blog โ€บ dictionary-comprehension
Dictionary Comprehension
January 19, 2023 - With the release of Python 2.7.3, PEP 274 introduced dictionary comprehension, which lets us simplify our code even further:
๐ŸŒ
DataCamp
datacamp.com โ€บ home โ€บ tutorials โ€บ python โ€บ python dictionary comprehension: essentials you need to know
Python Dictionary Comprehension Tutorial | DataCamp
December 4, 2024 - Learn all about Python dictionary comprehension: how you can use it to create dictionaries, to replace (nested) for loops or lambda functions with map(), filter() and reduce(), ...!
Find elsewhere
๐ŸŒ
Real Python
realpython.com โ€บ python-dictionary-comprehension
Python Dictionary Comprehensions: How and When to Use Them โ€“ Real Python
November 13, 2024 - In dictionary comprehension, the expression must provide the key and its corresponding value, key: value. Both elements can be expressions. The current member: This is the current item or value in the iterable.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ dictionary-comprehension-in-python-dict-comprehensions-explained
Dictionary Comprehension in Python โ€“ Dict Comprehensions Explained
September 16, 2022 - In the example above, we created a dictionary called developers which stores a list of developers with their favorite languages: {'Jane': 'Python', 'Jade': 'JavaScript', 'John': 'Python', 'Doe': 'JavaScript'}. In the dictionary comprehension, we added an expression at the end: if value == 'Python'.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_lists_comprehension.asp
Python - List Comprehension
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Python Dictionary Comprehensions - YouTube
Comprehensions are a powerful tool in Python that allow you to create new lists, sets and dictionaries, populated with values. This video explains how Dict c...
Published: May 9, 2023
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ datastructures.html
5. Data Structures โ€” Python 3.15.2 documentation
1 month ago - 5.1.5. Unpacking in Lists and List Comprehensions ยท 5.2. The del statement ยท 5.3. Tuples and Sequences ยท 5.4. Sets ยท 5.5. Dictionaries ยท 5.6. Looping Techniques ยท 5.7. More on Conditions ยท 5.8. Comparing Sequences and Other Types ยท 4. More Control Flow Tools ยท
๐ŸŒ
Yardsale8
yardsale8.github.io โ€บ stat489_book โ€บ AssociativeDataStructures_DictionariesAndSets โ€บ Comprehensions.html
5.3. Dictionary and Set Comprehensions โ€” Runestone Interactive Overview
We have seen how list comprehensions can used to describe and construct lists with an expression. Python has similar syntax for constructing dictionaries and sets using comprehensions.
๐ŸŒ
PhoenixNAP
phoenixnap.com โ€บ home โ€บ kb โ€บ devops and development โ€บ python dictionary comprehension explained
Python Dictionary Comprehension Explained | phoenixNAP KB
April 17, 2025 - Python 3 installed. An IDE or code editor to run code examples. Dictionary comprehension is a technique for creating Python dictionaries in one line. The method creates dictionaries from iterable objects, such as a list, tuple, or another dictionary.
๐ŸŒ
SitePoint
sitepoint.com โ€บ python hub โ€บ dictionary comprehension
Python - Dictionary Comprehension | SitePoint โ€” SitePoint
A dictionary comprehension is a concise way to create dictionaries in Python by specifying key-value pairs inside curly braces {} using a single line of code.
๐ŸŒ
CS50
cs50.harvard.edu โ€บ python โ€บ shorts โ€บ list_dictionary_comprehensions
List and Dictionary Comprehension - CS50's Introduction to Programming with Python
Interested in a verified certificate or a professional certificate ยท David J. Malan malan@harvard.edu Facebook GitHub Instagram LinkedIn Reddit X
๐ŸŒ
NobleBank
noblebank.com
A Community Bank in Central Alabama | NobleBank & Trust
Bank where they know your name. NobleBank & Trust is a community-owned Alabama bank with high-touch service and high-tech tools.
Price: $$
Address: 1509 Quintard Ave, 36201, Anniston
๐ŸŒ
Veritas Press
veritaspress.com โ€บ home
Veritas Press | K-12 Live Online Christian Education and Homeschooling
Classical Education from Veritas Press intends to be a academically rigorous, distinctively Christian, and methodically aligned with how God intended usโ€ฆ
๐ŸŒ
ListenData
listendata.com โ€บ home โ€บ python
Python Dictionary Comprehension with Examples
It includes various examples which would help you to learn the concept of dictionary comprehension and how it is used in real-world scenarios. ... Like list comprehension, dictionary comprehension lets us to run for loop with a single line of code.
๐ŸŒ
Alma Better
almabetter.com โ€บ bytes โ€บ tutorials โ€บ python โ€บ dictionary-comprehension-python
Dictionary Comprehension in Python
April 25, 2024 - Dictionary comprehension could be a Python feature that makes a new dictionary from an iterable object employing a brief syntax.