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.
We ran this 2-liner python code in Spyder 3 and got the dictionary printed as below.
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.
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.
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["One"] = [1111, 2222]
A = ['Dictionary', 'Of', 'Lists']
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.
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["One"] = [1111, 2222]
A = ['Its', 'First', 'List']
B = ['Its', 'Second', 'List']
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.
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.
for value in ['1', '2', '3'])
After the execution, we have got the below 3 key-pair dictionaries of lists.
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.