Python

Python Dictionary of Lists

Python is a diverse interpreter language used as an object-oriented approach. Dictionaries and lists are two common mutable used in the python language. Dictionaries keep data in “key-pair” format, i.e., only retrieved by its keys. While the list variables keep the bundle of data in a solitary mutable and only be retrieved by their index position. Within this guide today, we will see the different and simple methods to create a dictionary of lists in Python. For this purpose, we have been using the Python tool, i.e., Spyder3 in Windows 10. Let’s have a look.

Example 01:

Let’s take a first look at the simplest one. We have defined a Dictionary “Dic” with two key-par values. The key “Key1” contains a list of 2 integer values as its pair value, which applies to the “key2” element. Python’s print function is simply used to print the dictionary “Dic” in Spyder 3.

Dic = {"Key1": [10, 20], "key2": [32, 47]}

print(Dic)

We ran this 2-liner python code in Spyder 3 and got the dictionary printed as below.

Example 02:

Let’s exchange the key-pair location in this example. So, we used the list as a key and a string type value as a pair. After that, we have simply printed out this dictionary using the print function, taking the dictionary as an argument.

Dic = {[111, 222]: 'Hello'}

print(Dic)

After the code execution using the SPyder 3 run button, we have got the TypeError showing that the list is an unhashable type here in the dictionary. As the list is not immutable, the dictionary “Dic” doesn’t take it as a key, causing an exception.

Example 03:

We have seen how to create a dictionary of lists by taking the list as its direct value. We initialize an empty dictionary “Dic” and will pass a list as the value. So, we have declared “One” as the first key and initialized it with an integer type 2-member list as its pair value. Then, we have declared another key, “Two” and initialized it with a string type 3-member list. Now, the dictionary “Dic” has been filled with 2 key-pair values. The print function is to print the newly created dictionary “Dic” in the output. So, let’s check it.

Dic = {}

Dic["One"] = [1111, 2222]

A = ['Dictionary', 'Of', 'Lists']

Dic["One"].append(A)

print(Dic)

We have been using the same triangular button of the Spyder 3 python tool to run this code. In the output console screen, we have got the below output. It simply displayed the dictionary “Dic” with two keys having lists as their values. This is the simplest way to add a list as a key’s value in the dictionary.

Example 04:

We have started this example with the declaration of an empty dictionary, “Dic”. We have declared a key “One” in the dictionary “Dic” and initialized it with a list of numbers. Two lists, “A” and “B” of string types, have been declared and initialized separately. The list “A” has been appended in the key “One” of the dictionary “Dic” as a value, and the same has been done with the other list “B”. Now both lists are the value of key “One”. In last, the print function is printing the updated new dictionary “Dic” on the output screen.

Dic = {}

Dic["One"] = [1111, 2222]

A = ['Its', 'First', 'List']

B = ['Its', 'Second', 'List']

Dic["One"].append(A)

Dic["One"].append(B)

print(Dic)

After using the run button held at the top left corner of Spyder3, we have got the below output. It simply shows that the dictionary “Dic” has a single key-pair value. The “One” is a key, and two lists and two numbers are its totals of 4 values.

Example 05:

Here comes our last but not the unnecessary examples for creating a dictionary of lists. So, this will not be going to be the manual method as we used in the above examples. We will utilize the list comprehension technique of Python to create a dictionary of lists. So, we have started python code to initialize a dictionary variable “Dic”. This variable is taking a function “dict” as its value, creating a dictionary. The function dict utilizes the “for” loop to create a dictionary of a list. The “for” loop uses the integer “list” as its value. Until the loop ends, it will continue to iterate the values of the list to make a dictionary. The function “dict” will simply take two arguments. The “Value” is used to define the “key” and range is used to set the value of a key in the range of the list. The first value would be the same as the key, while the other will increment the first value. In the last, the dictionary has been printed out.

Dic = dict((value, range(int(value), int(value) + 2))

for value in ['1', '2', '3'])

print(Dic)

After the execution, we have got the below 3 key-pair dictionaries of lists.

Conclusion:

Finally! We have implemented creating a dictionary of lists in Python. We have implemented and covered the easiest and simplest methods to create a dictionary of the list in Python, i.e., list comprehension, direct submission, list as value, etc. We extremely hope and bluntly say that this article would be the best one for you.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content