How to create a dictionary from two lists in python

Create a dictionary from two lists in python :

In this python programming tutorial, we will learn how to create a dictionary from two different user input lists. Our program will ask the user to enter the values for both lists and then it will create one dictionary by taking the values. Values of the first list will be the key of the dictionary and corresponding values of the second list will be the value of the dictionary. Let’s take a look at the program :

Python program :

#1
first_list = []
second_list = []

#2
first_list_size = int(input("Enter total elements for the first list : "))
second_list_size = int(input("Enter total elements for the second list : "))

#3
for i in range(first_list_size):
    first_list.append(input("Enter value for the first list : "))

#4
for i in range(second_list_size):
    second_list.append(input("Enter value for the second list : "))

#5
print("Your first list : ",first_list)
print("Your second list : ",second_list)

#6
combined_dict = dict(zip(first_list,second_list))

#7
print("Final dictionary : ",combined_dict)

Explanation :

The commented numbers in the above program denote the step number below :

1. Create two empty lists first_list and second_list to store the values for the keys and values of the dictionary.
2. Ask the user to enter the size of both the lists. Read and store them in first_list_size and second_list_size variables.
3. Run one for loop. Take the values for the first list from the user and store them in first_list list.
4. Similarly, using one for loop, read and store the values for the second list in second_list variable.
5. Print out both lists to the user.
6. Using zip(), create one list of pairs from the lists. Then using dict(), convert that list of pairs to a dictionary.
7. Print out the created dictionary.

python create dictionary from two lists

Sample Output :

Enter total elements for the first list : 3
Enter total elements for the second list : 3
Enter value for the first list : 1
Enter value for the first list : 2
Enter value for the first list : 3
Enter value for the second list : first
Enter value for the second list : second
Enter value for the second list : third
Your first list :  ['1', '2', '3']
Your second list :  ['first', 'second', 'third']
Final dictionary :  {'1': 'first', '2': 'second', '3': 'third'}

Enter total elements for the first list : 3
Enter total elements for the second list : 5
Enter value for the first list : one
Enter value for the first list : two
Enter value for the first list : three
Enter value for the second list : sunday
Enter value for the second list : monday
Enter value for the second list : tuesday
Enter value for the second list : thursday
Enter value for the second list : friday
Your first list :  ['one', 'two', 'three']
Your second list :  ['sunday', 'monday', 'tuesday', 'thursday', 'friday']
Final dictionary :  {'one': 'sunday', 'two': 'monday', 'three': 'tuesday'}

Enter total elements for the first list : 3
Enter total elements for the second list : 2
Enter value for the first list : 1
Enter value for the first list : 2
Enter value for the first list : 3
Enter value for the second list : one
Enter value for the second list : two
Your first list :  ['1', '2', '3']
Your second list :  ['one', 'two']
Final dictionary :  {'1': 'one', '2': 'two'}

Conclusion :

Zipping is the easiest way to convert two lists to a dictionary. The first example we have shown above is a simple one, but the second and third examples are a little bit different. Both have different numbers of items in the lists. You can see that the final dictionary doesn’t contain all items.zip() ignores the items of a list if it doesn’t have anything corresponding in the other list. Go through the example and drop a comment below if you have any queries.

You might also like :

Leave a Reply