Python | Initializing dictionary with empty lists

11 May, 2020
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 comprehensionThis 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 constructnew_dict = {new_list: [] for new_list in range(4)}      # printing resultprint ("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 constructnew_dict = dict.fromkeys(range(4), [])      # printing resultprint ("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 defaultdictThis 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 defaultdictfrom collections import defaultdict  # initializing dict with listsnew_dict = defaultdict(list)  # performing append# shows no errornew_dict[0].append('GeeksforGeeks')      # printing resultprint ("New dictionary created : " + str(dict(new_dict)))
Output :
New dictionary created : {0: ['GeeksforGeeks']}

Method #4 : Using setdefaultsetdefault() can be used to perform this by specifying key-value pairs within a comprehension. This method obviates the need to import a module as is required in Method #3.
# Python3 code to demonstrate # to initialize dictionary with list # using setdefault  # initializing dict with listsnew_dict = {}[new_dict.setdefault(x, []) for x in range(4)]  # performing append# shows no errornew_dict[0].append('GeeksforGeeks')      # printing resultprint ("New dictionary created : " + str(dict(new_dict)))
Output :
New dictionary created : {0: ['GeeksforGeeks'], 1: [], 2: [], 3: []}

Method #5 : Using built-ins: dict and zipThe built-in functions dict, zip in conjunction with list comprehension can achieve the desired result.
# Python3 code to demonstrate # use of dict() and zip() built-ins to demonstrate# initializing dictionary with list   keys = range(4)new_dict = dict(zip(keys, ([] for _ in keys)))  print(new_dict)# performing append# shows no errornew_dict[0].append('GeeksforGeeks')      # printing resultprint ("New dictionary created : " + str(dict(new_dict)))
Output :
New dictionary created : {0: ['GeeksforGeeks'], 1: [], 2: [], 3: []}

ashok bakthavathsala
Python dictionary-programs
python-dict
Python
python-dict
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Iterate over a list in Python
How to iterate through Excel rows in Python?
Enumerate() in Python
Python OOPs Concepts
Different ways to create Pandas Dataframe
*args and **kwargs in Python
Python Classes and Objects
Introduction To PYTHON
Stack in Python
Queue in Python