{inner_k: myfunc(inner_v)} isn't a dictionary comprehension. It's just a dictionary.

You're probably looking for something like this instead:

data = {outer_k: {inner_k: myfunc(inner_v) for inner_k, inner_v in outer_v.items()} for outer_k, outer_v in outer_dict.items()}

For the sake of readability, don't nest dictionary comprehensions and list comprehensions too much.

Answer from Blender on Stack Overflow
🌐
OpenStax
openstax.org › books › introduction-python-programming › pages › 10-5-nested-dictionaries-and-dictionary-comprehension
10.5 Nested dictionaries and dictionary comprehension - Introduction to Python Programming | OpenStax
March 13, 2024 - A dictionary that contains only strings as keys and values. A dictionary that contains only lists as keys and values. ... Dictionary comprehension is a concise and efficient way to create a dictionary in Python.
Discussions

Nested Dictionary Comprehension
Use dict.items(), which lets you iterate through the keys and values of a dictionary at the same time: for key, value in some_dict.items(): print(f'{key}: {value}') So your comprehension would correspond to something like this: {key: [... comprehension over value ...] for key, value in email_dict['New ID'].items()} More on reddit.com
🌐 r/learnpython
5
1
June 6, 2018
How to construct nested dictionary comprehension in Python with correct ordering? - Stack Overflow
I was trying to shorten the code for this problem when I encountered the problem. Basically, I was trying a nested dictionary comprehension & was unsuccessful in the attempt. Here is what I tr... More on stackoverflow.com
🌐 stackoverflow.com
python - Nested dictionary comprehension (2 level) - Stack Overflow
I want to convert this to a nested dictionary. ... I know we can run a for loop and use setdefault to set/get paren_id value and then add the name and id as key, value · new_dic = {} for i in data: new_dic.setdefault(i['parent_id'], {})[i['name']] = i['id'] print(new_dic) But I am looking for a more pythonic way, meaning is it possible through dictionary comprehension... More on stackoverflow.com
🌐 stackoverflow.com
July 8, 2021
dictionary - Python nested dict comprehension - Stack Overflow
I have a dictionary: my_dict = {'a': [1, 2, 3], 'c': 3, 'b': 2} And I want a comprehension like add_dict = (x x +1 for x my_dict) what would be the best approach to take when writing a comprehensi... More on stackoverflow.com
🌐 stackoverflow.com
April 8, 2017
People also ask

What is dictionary comprehension in Python?

Dictionary comprehension is a concise and readable way to create dictionaries in Python. It provides a way to create a new dictionary from an iterable object like a list, tuple, or set.

🌐
datacamp.com
datacamp.com › home › tutorials › python › python dictionary comprehension: essentials you need to know
Python Dictionary Comprehension Tutorial | DataCamp
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
🌐
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(), ...!
🌐
Reddit
reddit.com › r/learnpython › nested dictionary comprehension
r/learnpython on Reddit: Nested Dictionary Comprehension
June 6, 2018 -

I am trying to do a nested dict. comprehension, but running into a syntaxerror.

What I want to do is remove the spaces in my lists. Sorry about the syntax, I had to space it all manually here, it's normal in my code:

email_dict=
{'New ID': 
    {'Fwd: RES': ['3', ' ', '4', ' ', '5'], 
    'RES CON': ['2', ' ', '1', '2'], 
    'RES EXP': [], 
    'RES SOLD': ['6', ' ', '1', '3']}
}

I tried to use this code to loop through each key, and find only the digits / remove the empty spots:

email_dict["New ID"] = {key for key in email_dict["New ID"]:[id for id in email_dict["New ID"][key] if id.isdigit()]}

I think I am really close, but not getting something here.

I can make it work when done this way:

for key in email_dict["New ID"]:
	temp_key = []
	for id in email_dict["New ID"][key]:
		if id.isdigit():
			temp_key += id
		email_dict["New ID"][key] = temp_key

But I'd prefer to learn how to do it a lot cleaner, and without the extra variable.

🌐
Engineering LibreTexts
eng.libretexts.org › bookshelves › computer science › programming languages › python programming (openstax) › 10: dictionaries
10.5: Nested Dictionaries and Dictionary Comprehension - Engineering LibreTexts
July 24, 2026 - ... b. A nested dictionary is a value of another dictionary, representing the data in a two-layer structure. Dictionary comprehension is a concise and efficient way to create a dictionary in Python.
🌐
Delft Stack
delftstack.com › home › howto › python › nested dictionary comprehension in python
Nested Dictionary Comprehension in Python | Delft Stack
February 22, 2025 - Dictionary comprehension is a very concise way to create a dictionary and is useful when you want to make a dictionary from a couple of tuples, where one tuple contains the key and value.
Find elsewhere
🌐
Programiz
programiz.com › python-programming › dictionary-comprehension
Python Dictionary Comprehension | Programiz
January 17, 2024 - We can add dictionary comprehensions to dictionary comprehensions themselves to create nested dictionaries.
🌐
Dataquest
dataquest.io › home › blog › python dictionary comprehension tutorial (with 39 code examples)
Python Dictionary Comprehension Tutorial (with 39 Examples)
March 6, 2023 - There is a way to rewrite the above for loop using dictionary comprehension. Do it for practice. In order to not complicate things, we will be using only the platform in the inner value. Very often, this is the format the data is delivered to us via API. # Create another nested dictionary nested_d = {} for n, dt in zip(name, date): nested_d[n] = {"date": dt} # Print the first five items of the dictionary print(list(nested_d.items())[:5])
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-dictionary-comprehension
Python Dictionary Comprehension - GeeksforGeeks
April 18, 2026 - We can include conditions in a dictionary comprehension to filter items or apply logic only to specific values. This allows us to create dictionaries more selectively. ... We can also create dictionaries within dictionaries using nested dictionary ...
🌐
EyeHunts
tutorial.eyehunts.com › home › nested dictionary comprehension python
Nested dictionary comprehension Python
December 21, 2023 - I am doing a birthday wisher, what I want in my program is that when today is equal to Key (month, day). For every key seach for a name and send a message. I thought the best way was to create a nested diccionary where I could search the key (month, day) and then name.
🌐
Rune Hub
rune.codes › home › tutorials › python › nested dictionaries in python
Nested Dictionaries in Python Explained | Rune Hub
July 8, 2026 - This creates inner dictionaries automatically when a new key is accessed. For JSON-like nested structures of fixed depth, a dictionary comprehension or a recursive function that builds the structure level by level is the clearest approach.
🌐
Learn By Example
learnbyexample.org › python-dictionary-comprehension
Python Dictionary Comprehension - Learn By Example
April 23, 2020 - The initial value in a dictionary comprehension can be any expression, including another dictionary comprehension. For example, here’s a simple list comprehension that uses a nested for clause.
🌐
Medium
medium.com › @joshuapaulrobin › dictionary-comprehension-in-python3-for-beginners-54fb4ddd3982
Dictionary Comprehension in Python 3 for Beginners | by Josh Robin | Medium
November 5, 2019 - Like list comprehensions, dictionary comprehensions can also be nested within each other. If we have a dictionary of dictionaries, we can modify them with nested dictionary comprehensions.
🌐
Analytics Vidhya
analyticsvidhya.com › home › python dictionary comprehension: mastering data manipulation with precision
Python Dictionary Comprehension: Mastering Data Manipulation with Precision
May 27, 2025 - For instance, suppose we have a list of cities and their populations. In this case, we can use nested dictionary comprehension to generate a dictionary with cities as keys and corresponding values as dictionaries containing information such as population and country.
🌐
Stack Overflow
stackoverflow.com › questions › 43137942 › python-nested-dict-comprehension
dictionary - Python nested dict comprehension - Stack Overflow
April 8, 2017 - Here's a one-liner for you (in Python 3), assuming the dictionary values never become double-nested: Copy>>> {k:([x+1 for x in v] if not isinstance(v,int) else v+1) for k,v in my_dict.items()} {'a': [2, 3, 4], 'b': 3, 'c': 4} Replace my_dict.items() with my_dict.iteritems() for Python 2 ... Thanks I had done it without comprehension which I guess was the first step.
🌐
Aliberro
aliberro.com › courses › python-fundamentals › dictionary-comprehension
Dictionary Comprehension - Python Fundamentals: From Zero to Hero | Ali Berro
January 9, 2026 - Given a list of strings, use dictionary comprehension to create a dictionary mapping each string to its length. ... Use nested dictionary comprehension to create a dictionary where keys are pairs (i, j) and values are i * j, for i and j from 0 to 2.
🌐
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 - Here we remove the dictionary to check what is actually happening in the loop. Now we see the problem! The issue is we have nested for loops. The loop says: for each name pair it with every country. Since dictionary keys can only appear, the value gets overwritten on each iteration.
🌐
Netguru
netguru.com › home page › blog › python dictionary comprehension: syntax, examples & pitfalls
Python dictionary comprehension: syntax, examples & pitfalls
August 14, 2026 - Here the outer comprehension iterates teams.items(), and each value is itself a fresh dictionary comprehension over members, no loop, no temporary list, just nested key-value pair construction.