Ways to Create a Dictionary of Lists in Python

Introduction:

In this tutorial, we are learning about to create a dictionary of Lists in Python. Dictionary in Python represents the collection of data stored in the form of key-value pairs. We may provide different information datatypes based on the value of a key. It helps coders store data and categories and create data accordingly. On the other hand, information is also stored in the list, but the content here is related to only a few values. The dictionary and the list are both indexed. In lists, we store data as arrays, and we can iterate and manipulate the arrays.

So far, we have seen different ways to create dictionaries and functions that consist of different keys and values in a Python dictionary. Now, let us look at different ways to create a list dictionary. Please note that the limitation of keys in a Python dictionary is that only immutable data types can be used as keys, i.e., we cannot use a dictionary of lists as keys.

Example:

Here, we give an example of the ways to create a dictionary of Lists in Python -

Program Code:

Here, we give a program code to show how we got type errors to create a dictionary of Lists in Python. The code is given below -

Output:

Now, we run the above code and find the result as a type error. The result is given below -

TypeError: unhashable type: 'list'

The ways to create a dictionary of Lists in Python:

There are various ways to create a dictionary of Lists in Python, which are given below -

  1. Using the subscript
  2. Using the setdefault() method
  3. Using the list comprehension
  4. Using the defaultdict
  5. Using the append() method
  6. Using the itertools module
  7. Using the Json.dumb() method

Now we learn about these approaches, which are given as follows -

1. Using the subscript:

This code starts with an empty dictionary, Dict. It then adds two key-value pairs to the dictionary: "name" has the value ["Rupa", "Priya"], and "age" has the value [23, 30]. Lastly, the code prints the dictionary's contents.

Program Code:

Here, we give a program to create a dictionary of Lists using the subscript in Python. The code is given below -

Output:

Now we run the above code and found a dictionary of Lists using the subscript in Python. The output is given below -

{'name': ['Rupa', 'Priya'], 'age': [23, 30]}

2. Using the setdefault() method:

The setdeafault() method is another way to create a dictionary of lists in Python. This method is used to return the dictionary value associated with the given key. The feature of this method is that if there is no unique key, it will create a unique key by adding it. Iterate through the list and use the setdefault() method to keep the elements up to the given range.

Program Code:

Here we give a program to create a dictionary of Lists using the setdeafault() method in Python. The code is given below -

Output:

Now we run the above code and find a dictionary of Lists using the setdeafault() method in Python. The output is given below -

The dictionary of list {10: ['10'], 11: ['10'], 12: ['10'], 40: ['40'], 41: ['40'], 42: ['40'], 20: ['20'], 21: ['20'], 22: ['20'], 60: ['60'], 61: ['60'], 62: ['60'], 80: ['80'], 81: ['80'], 82: ['80']}

3. Using the list comprehension:

List comprehension is another way to create a dictionary of lists in Python. In the list comprehension form, we iterate over the values '10', '40', '20', '60', and '80', and for each value, a key-value pair is created in the dictionary. The key is the current value, and the value is the range of numbers starting from the value converted to a number and ending with 3 plus that value.

Program Code:

Here, we give a program to create a dictionary of Lists using the list comprehension in Python. The code is given below -

Output:

Now we run the above code and found a dictionary of Lists using the list comprehension in Python. The output is given below -

The dictionary of list {'10': range(10, 13), '40': range(40, 43), '20': range(20, 23), '60': range(60, 63), '80': range(80, 83)}

4. Using the defaultdict:

The defaultdict is another way to create a dictionary of lists in Python. Here, we import the defaultdict class from the Collections module. It defines a list of tuples, each containing a key-value pair. Create a defaultdict object named d_Dict that will contain an empty list of values. Use a loop to iterate over the list of tuples and unpack each tuple into key and val variables. Then, append the value Val to the list associated with the key in the d_Dict dictionary.

Program Code:

Here, we give a program to create a dictionary of Lists using the defaultdict in Python. The code is given below -

Output:

Now we run the above code and find a dictionary of Lists using the defaultdict in Python. The output is given below -

The value of default dictionary defaultdict(<class 'list'>, {'Welcome': [1], 'to': [2], 'javatpoint': [3]})

5. Using the append() method:

The append() method is another way to create a dictionary of lists in Python. Use the append() method to append nested lists as values. Create a new list, and we will add it to the value.

Program Code:

Here we give a program to create a dictionary of Lists using the append() method in Python. The code is given below -

Output:

Now, we run the above code and find a dictionary of Lists using the append() method in Python. The output is given below -

The dictionary of list {'key': [1, 2, 3, ['Riye', 'Priya', 'Hiya']]}

6. Using the itertools module:

The itertools module is another way to create a dictionary of lists in Python. One way is to use the zip_longest function in the itertools module. This function allows you to repeat two or more iterables elements together and fill in the missing elements with the fill value.

Program Code:

Here we give a program to create a dictionary of Lists using the itertools module in Python. The code is given below -

Output:

Now, we run the above code and find a dictionary of Lists using the itertools module in Python. The output is given below -

The dictionary of list {10: [18, 'Riya'], 30: [25, 'Priya'], 60: [30, 'Hiya'], 20: [45, 'Diya'], 40: [56, 'Esha'], 50: [72, 0], 0: [23, 0]}

7. Using the Json.dumb() method:

The Json.dumb() method is another way to create a dictionary of lists in Python. By importing the JSON module, you initialize a list containing tuples. Then, start with an empty dictionary. The code uses the json.dumps() function to convert the list to a JSON-formatted string and assign the string as a key with the value "converted" in the dict dictionary. Finally, the dictionary is printed.

Program Code:

Here, we give a program to create a dictionary of Lists using the Json.dumb() method in Python. The code is given below -

Output:

Now, we run the above code and find a dictionary of Lists using the Json.dumb() method in Python. The output is given below -

The dictionary of list {'[["Riya", 10], ["Priya", 20], ["Hiya", 30]]': 'converted'}

Conclusion:

In this tutorial we are learning about the ways to create a dictionary of Lists in Python. Here, we learn various ways to create a dictionary of Lists in Python, along with some suitable examples.