You can use collections.defaultdict for a nested dictionary, where you define an initial value of a dictionary

from collections import defaultdict

#Use the initial value as a dictionary
dct = defaultdict(dict)

dct['a']['b'] = 'c'
dct['d']['e'] = 'f'

print(dct)

The output will be

defaultdict(<class 'dict'>, {'a': {'b': 'c'}, 'd': {'e': 'f'}})
Answer from Devesh Kumar Singh on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ initialize-an-empty-dictionary-in-python
Initialize an Empty Dictionary in Python - GeeksforGeeks
July 15, 2025 - This code demonstrates how to create an empty dictionary using a dictionary comprehension. It initializes d with no elements and then prints the dictionary, its length, and its type.
Discussions

Why is it common in python to make an empty list or dict first?
In your case itโ€™s completely useless however many times we run a loop and append to a list or add to a variable counter or many such things. This has to exist before we do so. I mean I thought a lot of list comprehension was supposed to help mitigate this very thing lol. for element in some_list: my_list.append(element*2) Will fail. More on reddit.com
๐ŸŒ r/Python
157
165
September 19, 2023
Initializer for set, like dict {} and list []
Quite often I find myself using sets, and I cannot initialize them, like you can a dict, list or a tuple. This makes uglier code: a_dictionary = {} a_tuple = () a_list = [] a_set = set() Also set comprehension uses {} as a dict: a_set = {'unique_1', 'unique_2'} I propose that set gets the unique ... More on discuss.python.org
๐ŸŒ discuss.python.org
1
December 4, 2024
Initializing a dictionary in python with a key value and no corresponding values - Stack Overflow
I setup my data and an empty dictionary. I then loop through a list of strings that represent potential keys in my data dictionary. I copy each value from data to my_data, but consider the case where data may not have the key that I want. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... If context is king, architecture is the... AI agents are a confused deputy with the keys to your... ... 0 How can I initialize ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Creating a new dictionary in Python - Stack Overflow
I want to build a dictionary in Python. However, all the examples that I see are instantiating a dictionary from a list, etc . .. How do I create a new empty dictionary in Python? More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ empty-dictionary-python
Create Empty Dictionary in Python (5 Easy Ways) | FavTutor
April 13, 2022 - If no arguments are provided, an empty dictionary is created. ... In Python, we can use the zip() and len() methods to create an empty dictionary with keys.
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ why is it common in python to make an empty list or dict first?
r/Python on Reddit: Why is it common in python to make an empty list or dict first?
September 19, 2023 -

Something I've never understood with python is why it is so common for an empty list to be created and then populated with something in a subsequent (or even more common next) line:

my_list = []
my_list = [i for i in range(1, 21)]

Obviously this is a really really toy example, but I'm consistently finding this in "production" code in multiple places. Is it pythonic? It's basically pure duplication so I doubt it. Is it some hacky "optimisation"? Is it a side effect of the code priorly being a for loop, then turned to a list comprehension but not tidied up right?

I honestly just feel ike I'm missing something key, with the amount of times and variety of places I see this pattern.

๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
Add the ability to declare an empty dict as `{:}` - Page 2 - Ideas - Discussions on Python.org
March 29, 2023 - In my opinion, the fact that {} declares an empty dict is a major design flaw. Empty curly braces should only be associated with the empty set. If a newcomer learns that [] declares an empty list, their intuition would โ€ฆ
๐ŸŒ
Quora
quora.com โ€บ How-do-I-create-an-empty-dictionary-in-Python
How to create an empty dictionary in Python - Quora
Answer (1 of 13): * In Python to create an empty dictionary, we can assign no elements in curly brackets {}. * We can also create an empty dictionary by using the dict () method it is a built-in function in Python and takes no arguments.
Find elsewhere
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ create-a-dictionary-in-python-python-dict-methods
Create a Dictionary in Python โ€“ Python Dict Methods
March 14, 2022 - Another way of creating an empty dictionary is to use the dict() function without passing any arguments.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ dictionary-dict-function
Python Dictionary: Syntax and Examples [Python Tutorial]
You can also create a dictionary using the dict() function and passing key-value pairs as keyword arguments. Using dict() without any arguments creates an empty dictionary.
๐ŸŒ
Namehero
namehero.com โ€บ blog โ€บ how-to-initialize-a-dictionary-in-python
How to Initialize a Dictionary in Python
November 7, 2024 - The two curly brackets {} signify a dictionary. You can also initialize a dictionary like this: ... The โ€œdict()โ€ function is a constructor that we can use to either create an empty dictionary or one pre-filled with data.
๐ŸŒ
Index.dev
index.dev โ€บ blog โ€บ initialize-dictionary-with-empty-lists-python
How to Initialize a Dictionary with Empty Lists in Python [with Examples]
Learn how to initialize a dictionary with empty lists in Python. This guide covers practical examples and common use cases for creating and managing dictionaries where each key is associated with an empty list.
๐ŸŒ
Medium
sokacoding.medium.com โ€บ 6-ways-to-initialize-a-dictionary-in-python-e9a0b1a5572e
6 Ways to initialize a Dictionary in Python | by sokacoding | Medium
January 24, 2023 - For the sake of completeness, I show you how to use dictionary comprehension if we have a list of tuples with our sample data to initialize a dictionary.
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
Initializer for set, like dict {} and list []
December 4, 2024 - Quite often I find myself using sets, and I cannot initialize them, like you can a dict, list or a tuple. This makes uglier code: a_dictionary = {} a_tuple = () a_list = [] a_set = set() Also set comprehension uses {} as a dict: a_set = {'unique_1', 'unique_2'} I propose that set gets the unique bracket style Code will look like so: a_dictionary = {} a_tuple = () a_list = [] a_set = And for comprehension it would look like so: a_set = And this would make...
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ how to initialize dictionary in python โ€“ a step-by-step guide
How to Initialize Dictionary in Python - A Step-By-Step Guide - AskPython
September 24, 2022 - We can use setdefault() method to initialize dictionary in python as follows. This method returns the value of a key within a list comprehension. Let us create an empty dictionary named my_dictionary and using setdefault() method to assign null ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-initialize-list-with-empty-dictionaries
Python | Initialize list with empty dictionaries - GeeksforGeeks
March 24, 2023 - ... # Python3 code to demonstrate working of # Initialize list with empty dictionaries # using {} + &quot;*&quot; operator # Initialize list with empty dictionaries # using {} + &quot;*&quot; operator res = [{}] * 6 print(&quot;The list of empty ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-initializing-dictionary-with-empty-lists
Initializing dictionary with Empty Lists in Python - GeeksforGeeks
July 11, 2025 - Dictionary comprehension is the most sought method to do this initialization. In this method, we create the no. of keys we require and then initialize the empty list as we keep on creating the keys, to facilitate the append operation afterward without an error. ... # Python3 code to demonstrate # to initialize dictionary with list # using dictionary comprehension # using dictionary comprehension to construct new_dict = {new_list: [] for new_list in range(4)} # printing result print ("New dictionary with empty lists as keys : " + str(new_dict))
๐ŸŒ
Google Groups
groups.google.com โ€บ g โ€บ dev-python โ€บ c โ€บ DZM0y_RecB0
[Python-Dev] An unambiguous way of initializing an empty dictionary and set
March 13, 2022 - Oops, didnโ€™t notice this wasnโ€™t Python-ideas โ€” thatโ€™s where it should be. ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... On 3/13/2022 5:49 PM, joao.p.f....@gmail.com wrote: > Currently: > l = [] # new empty list > t = () # new empty tuple > s = set() # new empty set (no clean and consistent way of initializing regarding the others) <<< > d = {} # new empty dictionary > > Possible solution: > s = {} # new empty set > d = {:} # new empty dictionary (the ":" is a reference to key-value pairs) This is such a good idea that many people, including myself, have already had it, and even more agree that this would be the proper way for a new language.