This tutorial will discuss about unique ways to convert a list of lists to a dictionary in Python.
Table Of Contents
Suppose we have a list of lists, in which each sublist has two elements i.e. a Name and a number i.e.
# List of Lists listOfLists = [ ['John', 34], ['Mark', 23], ['Suse', 37], ['Avni', 29], ['John', 100], ['John', 102]]
Now we want to convert this list of lists into a dictionary. Where,
- First element of each sublist from list, will be the key in dictionary.
- Second element of each sublist from list, will be the value in dictionary.
If there are duplicate entried for a key, then the all values of those keys will be stored in a list. For example, above list of lists will be converted into a dictionary like this,
{'John': [34, 100, 102], 'Mark': 23, 'Suse': 37, 'Avni': 29}
As John
was present in three sublists, so we need to construct a list as its value in the dictionary.
There are different ways to do this. Let’s dicuss them one by one.
Frequently Asked:
Method 1: Using for loop
Steps are as follows,
- Create an empty dictionary
- Iterate over all sublists in list, and for each sublist,
- Select first value of sublist, and check if it exists in dictionary or not,
- If not, then add that to the dictionary as key, and use the second value of sublist as value.
- If yes, and type of its value is list, then add second value of sublist in that list.
- If yes, and type of its value is not list, then create an empty list,
- Add existing value for that key in that list.
- Add second value of sublist in that list.
- Make that list as value of key.
- Select first value of sublist, and check if it exists in dictionary or not,
It will convert a list of sublists to a dictionary, in which we can have multiple values for each key.
Let’s see the complete example,
# List of Lists listOfLists = [ ['John', 34], ['Mark', 23], ['Suse', 37], ['Avni', 29], ['John', 100], ['John', 102]] dictObj = {} # Iterate over all sublists for (firstValue, secondValue) in listOfLists: # If first value of sublist is not in dict # then add it if firstValue not in dictObj: dictObj[firstValue] = secondValue elif isinstance(dictObj[firstValue], list): # If key already exist and value is of list type, # then append value in list dictObj[firstValue].append(secondValue) else: # If key already exist and value is not of list type, # then create a list and add both the values in it dictObj[firstValue] = [dictObj[firstValue]] dictObj[firstValue].append(secondValue) print(dictObj)
Output
{'John': [34, 100, 102], 'Mark': 23, 'Suse': 37, 'Avni': 29}
Method 2: Using setdefault()
If you want each value of the dictionary to be a list, so that it can accommodate multiple values for a key. Then you can use the setdefault()
method of dictionary.
Iterate over all sublists in list, and add them to dictionary. For each sublists,
- Use the first value of sublists as key field of dictionary entry.
- Use the second value of sublists as value field of dictionary entry.
If each key use the default value as list, so if the key doesn’t exist in list yet, then while adding its default value be a list. Then append the values to that list.
Let’s see the complete example,
# List of Lists listOfLists = [ ['John', 34], ['Mark', 23], ['Suse', 37], ['Avni', 29], ['John', 100], ['John', 102]] dictObj = {} # Convert a list of lists to a dictionary for firstValue, secondValue in listOfLists: # Set default value of each key as list # and append values in that list dictObj.setdefault(firstValue, []).append(secondValue) print(dictObj)
Output
Python List Methods
{'John': [34, 100, 102], 'Mark': [23], 'Suse': [37], 'Avni': [29]}