In this sample program, you will learn how to convert lists to a Python dictionary and show it using the print() function. To understand this demo program, you should have basic Python programming knowledge.
However, here we’ll use the following steps to convert lists into a dictionary.
- Define a pair of lists: one for the keys and one for the values.
- Set the lists as empty which means they should not have any elements.
- Then the program asks for the following inputs from the user.
- First, it gets the number of elements that you want in the list.
- After that, it uses a for loop which asks the user to supply inputs and get them added to the list of keys.
- Next is another for loop which receives values from the end-user and keeps adding them to the list of values in each iteration.
- Finally, we call the Python zip method to convert lists into a dictionary.
- In the end, print the dictionary object.
Below is the sample code of the Python Program to convert lists into a dictionary using the Zip() method.
Python Program – Convert Lists into the Dictionary
You can use IDLE or any other Python IDE to create and execute the below programs.
Using the for loop to convert lists into a dictionary
# Program to Convert Lists into a Dictionary # Pair of lists for storing the keys and values listOfkeys = [] listOfvalues = [] count = int(input("Input total no. of elements in the lists:")) print("Capture input for the keys:") for item in range(0, count): elt = int(input("Input item" + str(item + 1) + ":")) listOfkeys.append(elt) print("Capture input for the values:") for item in range(0, count): elt = int(input("Input item" + str(item + 1) + ":")) listOfvalues.append(elt) di = dict(zip(listOfkeys, listOfvalues)) print("The dictionary after the merge:") print(di)
The output of the above code is as follows.
Input total no. of elements in the lists:5 Capture input for the keys: Input item1:1 Input item2:2 Input item3:3 Input item4:4 Input item5:5 Capture input for the values: Input item1:11 Input item2:22 Input item3:33 Input item4:44 Input item5:55 The dictionary after the merge: {1: 11, 2: 22, 3: 33, 4: 44, 5: 55}
Using list comprehension
Create a dictionary from two separate lists, one containing keys and the other containing corresponding values. This example demonstrates how to use a list comprehension and the zip() function to pair the elements.
keys = ['a', 'b', 'c'] values = [1, 2, 3] dictionary = {key: value for key, value in zip(keys, values)} print(dictionary) # {'a': 1, 'b': 2, 'c': 3}
Using enumerate with list comprehension
Convert a list into a dictionary where the list elements become keys, and their corresponding indices serve as values. This is achieved using enumerate() and a list comprehension.
my_list = ['apple', 'banana', 'cherry'] dictionary = {value: index for index, value in enumerate(my_list)} print(dictionary) # {'apple': 0, 'banana': 1, 'cherry': 2}
By the way, if you are seeking to learn more or planning for a job change, go through these 100+ Python interview questions. It will certainly rejuvenate your Python programming skills.