Use a dict comprehension (Python 2.7 and later):

{key: value for key, value in zip(keys, values)}

Alternatively, use the dict constructor:

pairs = [('a', 1), ('b', 2)]
dict(pairs)                          # โ†’ {'a': 1, 'b': 2}
dict((k, v + 10) for k, v in pairs)  # โ†’ {'a': 11, 'b': 12}

Given separate lists of keys and values, use the dict constructor with zip:

keys = ['a', 'b']
values = [1, 2]
dict(zip(keys, values))              # โ†’ {'a': 1, 'b': 2}
Answer from fortran on Stack Overflow
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-dictionary-comprehension
Python Dictionary Comprehension Tutorial | DataCamp
December 4, 2024 - Learn all about Python dictionary comprehension. Learn how you can use it to create dictionaries, to replace (nested) for loops or lambda functions with map(), filter(), reduce(), and more! ... Dictionaries (or dict in Python) are a way of storing elements just like you would in a Python list. But, rather than accessing elements using its index, you assign a fixed key to it and access the element using the key. What you now deal with is a "key-value" pair, which is sometimes a more appropriate data structure for many problems instead of a simple list.
๐ŸŒ
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.
Discussions

Python Dictionary Comprehension - Stack Overflow
Is it possible to create a dictionary comprehension in Python (for the keys)? Without list comprehensions, you can use something like this: ... We can shorten this to a list comprehension: l = [n for n in range(1, 11)]. However, say I want to set a dictionary's keys to the same value. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Dictionary Comprehension not iterating over all of list
Dict's keys are unique. Old values are overwritten with new ones when key is the same. Maybe use "id"? Also it's said to iterate like this here, without range in cases like this: test = {entry["id"]: entry["completed"] for entry in my_list} More on reddit.com
๐ŸŒ r/learnpython
6
9
November 21, 2022
dictionary comprehension -- order of keys created?
What you are creating are sets, not dictionaries. And to the best of my knowledge, the python set container makes no guarantees regarding order. It may not even be the same on every run? There is such a thing as a dictionary comprehension though. For example, you could write: >>> {x: x * 2 for x in str(90210)} {'9': '99', '0': '00', '2': '22', '1': '11'} As of python 3.6 (I think?), the dict container does try to maintain the order in which items are inserted. More on reddit.com
๐ŸŒ r/learnpython
5
1
April 6, 2023
List comprehension unpacking JSON: handling missing values (KeyError)
Let's say you have some data like the following and you want to collect all the x values, with a list comprehension: results = [ {'x': 1 }, {'x': 2 }, {'y': 3 }, {'x': 4 }, {'z': 5 } ] Using dict.get(key) (in opposition to dict[key]) doesn't rise a KeyError if the key does not exists in the dictionary, it just returns None (or a default you can specify), so you could do: [x for x in (i.get('x') for i in results) if x is not None] // == [1,2,4] note: just from the top of my mind, no considerations about performance ecc More on reddit.com
๐ŸŒ r/learnpython
3
1
January 23, 2023
๐ŸŒ
PhoenixNAP
phoenixnap.com โ€บ home โ€บ kb โ€บ devops and development โ€บ python dictionary comprehension explained
Python Dictionary Comprehension Explained | phoenixNAP KB
April 17, 2025 - The method creates key-value pairs, where the keys are numbers, and the values are their squares. Alternatively, create a list with strings and use dictionary comprehension to map the number of characters to each string.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ dictionary-comprehension-in-python-explained-with-examples
Dictionary Comprehension in Python โ€“ Explained with Examples
August 24, 2021 - You can use my_dict["key1"] to access <value1>, my_dict["key2"] to access <value2>, and so on. Now that we know what a Python dictionary is, let's go ahead and learn about Dictionary Comprehension.
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ what-is-dictionary-comprehension-in-python
What Is Dictionary Comprehension in Python? | Codecademy
Letโ€™s walk through the parameters of dictionary comprehension: key_expression: Determines each key in the dictionary. value_expression: Defines the value associated with each key.
๐ŸŒ
Real Python
realpython.com โ€บ python-dictionary-comprehension
Python Dictionary Comprehensions: How and When to Use Them โ€“ Real Python
October 14, 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.
Find elsewhere
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ python dictionary comprehension: mastering data manipulation with precision
Python Dictionary Comprehension: Mastering Data Manipulation with Precision
May 27, 2025 - Here are some methods and examples of using dictionary comprehension in Python: Dictionary comprehension follows the syntax `{key: value for (key, value) in iterable}`. It iterates over each item in the iterable and constructs a dictionary where ...
๐ŸŒ
Switowski
switowski.com โ€บ blog โ€บ dictionary-comprehension
Dictionary Comprehension - Sebastian Witowski
January 19, 2023 - We can simplify our code by passing a list of key-value tuples directly to the dict() function: ... Before Python 2.7, this was the simplest way to build a dictionary from an iterable. It's not bad, but all those brackets and parentheses can be slightly confusing. With the release of Python 2.7.3, PEP 274 introduced dictionary comprehension, which lets us simplify our code even further:
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ dictionary-comprehension-in-python-dict-comprehensions-explained
Dictionary Comprehension in Python โ€“ Dict Comprehensions Explained
September 16, 2022 - In this section and the next, you'll learn about other expressions that you can use to modify the items stored in dictionaries created using dictionary comprehension. We'll start with conditional statements. developers = {'Jane': 'Python', 'Jade': 'JavaScript', 'John': 'Python', 'Doe': 'JavaScript'} python_developers = {key: value for (key, value) in developers.items() if value == 'Python'} print(python_developers) # {'Jane': 'Python', 'John': 'Python'}
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-dictionary-comprehension
Python Dictionary Comprehension
August 18, 2025 - This lets you generate key-value pairs dynamically in a single line of code. Letโ€™s say I have a list of U.S. states and I want to assign each state a code. Instead of writing a loop, I can use dictionary comprehension in Python.
๐ŸŒ
Medium
medium.com โ€บ swlh โ€บ dictionary-comprehension-in-python3-for-beginners-54fb4ddd3982
Dictionary Comprehension in Python 3 for Beginners | by Josh Robin | Medium
November 5, 2019 - The major difference in this syntax from a list comprehension is that instead of iterating over one group of values, the dictionary comprehension is iterating over two groups in tandem, a group of keys and a group of values. This is why there are two iterating variables in this case, key, value, which translate to a final key: value pair.
๐ŸŒ
Dataquest
dataquest.io โ€บ home โ€บ blog โ€บ python dictionary comprehension tutorial (with 39 code examples)
Python Dictionary Comprehension Tutorial (with 39 Examples)
March 6, 2023 - Another use of dictionary comprehension in Python is dictionary sorting. For example, let's say we want to sort the dictionary with game titles and years by year from the first to the latest year? Let's first transform years from str data type into int data type (using comprehension!). # Convert years from strings into intergers year_dict_int = {key: int(value) for key, value in year_dict.items()} # Sort by year sorted_year = {key: value for key, value in sorted(year_dict_int.items(), key=lambda x: x[1])} # Print the first five items of the dictionary sorted by year print(list(sorted_year.items())[:5])
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ dictionary-comprehension
Python Dictionary Comprehension: Syntax, Usage, and Examples
Create dictionaries fast with Python dictionary comprehension using {key: value for ...} syntax, letting you transform and filter key-value pairs and build lookup tables in a single readable line.
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ 10 examples to master python dictionary comprehensions
10 Examples to Master Python Dictionary Comprehensions | Towards Data Science
January 18, 2025 - ... print(dct) {(1, 5): 5, (1, ... 7): 28} Each pair of items in the lists is a key in the dictionary. The value is the product of the items in keys. ... Dictionaries are very important data structures in Python and used ...
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ python basics โ€บ python dictionary comprehension
Python Dictionary Comprehension
March 27, 2025 - A dictionary comprehension allows you to run a for loop on a dictionary and do something on each item like transforming or filtering and returns a new dictionary. Unlike a for loop, a dictionary comprehension offers a more expressive and concise syntax when you use it correctly.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python dictionary comprehension explained
Python Dictionary Comprehension Explained - Spark By {Examples}
May 31, 2024 - In dictionary comprehension, we need to specify both keys and values based on the iteration. The returned dictionary contains the elements as keys and their divisions as values. We can access the key-value pairs in a dictionary by using the ...
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)
{}
๐ŸŒ
Tabnine
tabnine.com โ€บ home โ€บ the ultimate guide to python dictionary comprehension
The ultimate guide to Python Dictionary comprehension - Tabnine
July 25, 2024 - In a way, itโ€™s like copying your original dictionary and doing what you need to do with the values before your final dictionary is set. To do this, you will need to be able to access the key and the value within each dictionary object. This is achievable using .keys() and .values().
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ python dictionary comprehension: a powerful one-liner tutorial
Python Dictionary Comprehension: A Powerful One-Liner Tutorial - Be on the Right Side of Change
September 27, 2022 - Letโ€™s consider five examples of dictionary comprehensions to strengthen your understanding! The examples are improved and simplified versions of the code given here. Problem: create a dict comprehension from a list of integers. # Example 1: # (key, value) --> (string, int) print({str(i):i for i in range(5)}) # {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4}