Python | Initializing dictionary with empty lists
In python one usually comes across situations in which one has to use dictionary for storing the lists. But in those cases, one usually checks for first element and then creates a list corresponding to key when it comes. But its always wanted a method to initialize the dict. keys with list. Let’s discuss certain ways to achieve this particular task.
Method #1 : Using Dictionary comprehension
This is most sought of 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, so as to facilitate the append operation afterwards 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)) |
Output :
New dictionary with empty lists as keys : {0: [], 1: [], 2: [], 3: []}
Method #2 : Using fromkeys()
fromkeys() can be used to perform this by specifying the additional empty list as argument and the range of elements which need to be the key of dictionary being made.
# Python3 code to demonstrate # to initialize dictionary with list # using fromkeys() # using fromkeys() to construct new_dict = dict.fromkeys(range(4), []) # printing result print ("New dictionary with empty lists as keys : " + str(new_dict)) |
Output :
New dictionary with empty lists as keys : {0: [], 1: [], 2: [], 3: []}
Method #3 : Using defaultdict
This is most pythonic way and error free way to use any key without initialization of its value, it has to be told the type of default container of all its keys and then evaluates the operations and structures accordingly.
# Python3 code to demonstrate # to initialize dictionary with list # using defauldict from collections import defaultdict # initializing dict with lists new_dict = defaultdict(list) # performing append # shows no error new_dict[0].append('GeeksforGeeks') # printing result print ("New dictionary created : " + str(dict(new_dict))) |
Output :
New dictionary created : {0: ['GeeksforGeeks']}
Recommended Posts:
- Python | Initializing dictionary with list index values
- Python | Initializing multiple lists
- Python | Check if dictionary is empty
- Python | Convert two lists into a dictionary
- Python | Iterate through value lists dictionary
- Python | Ways to create a dictionary of Lists
- Python | Split dictionary of lists to list of dictionaries
- Python | Convert list of tuples to dictionary value lists
- Python | Split dictionary keys and values into separate lists
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Create pandas dataframe from lists using dictionary
- Python | Convert string dictionary to dictionary
- Python | Program to count number of lists in a list of lists
- Python | Zipping two lists of lists
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



