{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 - Explain the structure of nested dictionaries. Use dictionary comprehension to create a dictionary object. As described before, Python dictionaries are a type of data structure that allows for storing data in key-value pairs.
Discussions

Dict unpacking in dict comprehensions
It is a bit noisy for what it does, and mildly confusing. I don't know a more idiomatic way. Stackoverflow is full of ** about it too. Leanest py3 oneliner I can come up with is this: merged = dict(i for i in d.items() for d in dict_list) More on reddit.com
🌐 r/Python
10
9
April 13, 2016
Nested Dictionary Comprehension

Bonus question: am I wasting my time even trying to do this, since the nested for loop works fine?

More on reddit.com
🌐 r/learnpython
7
1
December 29, 2020
At what point are loops better than list comprehensions?
Readability is important, just after correctness. That means, in the initial approximation, speed/efficiency is less important. Until much later anyway. I wouldn't try to write that code as a comprehension. If, later, you need faster code you worry about algorithms first, not the low-level stuff. More on reddit.com
🌐 r/learnpython
56
32
September 19, 2023
Nested Dictionary Comprehension From Dataframe?
sorted_DF.to_dict("index") https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_dict.html More on reddit.com
🌐 r/learnpython
6
1
April 6, 2021
🌐
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.

🌐
DataCamp
datacamp.com › tutorial › python-dictionary-comprehension
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(), ...!
🌐
datagy
datagy.io › home › python posts › python dictionary comprehensions (with examples)
Python Dictionary Comprehensions (With Examples) • datagy
September 16, 2023 - It’s important to note that for nested dictionary comprehensions, Python starts with the outer loop and then moves (per item) into the inner loop.
🌐
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.
🌐
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])
Find elsewhere
🌐
Programiz
programiz.com › python-programming › dictionary-comprehension
Python Dictionary Comprehension
Whenever nested dictionary comprehension is used, Python first starts from the outer loop and then goes to the inner one.
🌐
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 ...
🌐
Real Python
realpython.com › python-dictionary-comprehension
Python Dictionary Comprehensions: How and When to Use Them – Real Python
October 14, 2024 - Here’s a diagram that illustrates the process of converting your loop from the previous section into the equivalent dictionary comprehension: As you can see, with a couple of movements and the addition of the enclosing curly braces, your comprehension is complete. Comprehensions can also have more than one for clause. When they do, the leftmost for iterates over the outer collection, the next for from left to right iterates over the first nesting level, and so on.
🌐
Engineering LibreTexts
eng.libretexts.org › bookshelves › computer science › programming languages › python programming (openstax) › 10: dictionaries
10.5: Nested Dictionaries and Dictionary Comprehension - Engineering LibreTexts
April 25, 2026 - ... Dictionary comprehension is a concise and efficient way to create a dictionary in Python. With dictionary comprehension, elements of an iterable object are transformed into key-value pairs.
🌐
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.
🌐
Tabnine
tabnine.com › home › the ultimate guide to python dictionary comprehension
The ultimate guide to Python Dictionary comprehension - Tabnine
July 25, 2024 - This is the barebones basics of python dictionary comprehension. The next step is to explore conditions, such as if conditions and using multiple if conditions. Beyond this is nested dictionary comprehension — which poses its challenges due to the nested nature.
🌐
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 - The outer_val is actually its own dictionary, which we are doing a dictionary comprehension on for each inner dictionary. That is nested dictionary comprehensions in a nutshell.
🌐
Fiveable
fiveable.me › all study guides › intro to python programming › unit 10 – dictionaries study guides › topic: 10.5
Nested dictionaries and dictionary comprehension | Intro... | Fiveable
June 24, 2024 - Nested dictionaries in Python allow ... and can be modified or retrieved with various methods. Dictionary comprehension offers a concise way to create dictionaries from existing iterables....
🌐
Sololearn
sololearn.com › en › Discuss › 1267228 › nested-dictionary-and-comprehension-in-python
Nested dictionary and comprehension in python | Sololearn: Learn to code for FREE!
i have a nested dictionary in the following form: dict = {"0":{ }, "1":{ }, "2":{ }, "3":{ }, ... } the inner dictionaries have various keys, not necessarily uniform, eg: dict["0"] = {"name" : "bruce", "age" : 28, ...} dict["1"] = {"name" : ...
🌐
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.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python dictionary comprehension explained
Python Dictionary Comprehension Explained - Spark By {Examples}
May 31, 2024 - The nesting concept always starts the outer loop and then goes to the inner loop. When you need to convert a list to a dictionary, we need the key: value pairs, by applying enumerate() to the list we can get the index of list items.
🌐
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.
🌐
Scaler
scaler.com › home › topics › python dictionary comprehension
Python Dictionary Comprehension - Scaler Topics
March 22, 2024 - This method efficiently filters ... comprehensions. Nested dictionary comprehension involves creating a dictionary with dictionaries as its values, using a single expression....