Write a Python Program to Map two lists into a Dictionary with a practical example.
Python Program to Map two lists into a Dictionary Example 1
In this python program, we are using for loop with zip function.
# Python Program to Map two lists into a Dictionary keys = ['name', 'age', 'job'] values = ['John', 25, 'Developer'] myDict = {k: v for k, v in zip(keys, values)} print("Dictionary Items : ", myDict)
Python Program to insert two lists into a Dictionary Example 2
This Python code is another approach to insert lists into a Dictionary. In this program, we are using a dict keyword along with zip function.
# Python Program to Map two lists into a Dictionary keys = ['name', 'age', 'job'] values = ['John', 25, 'Developer'] myDict = dict(zip(keys, values)) print("Dictionary Items : ", myDict)
Program to map two lists into a Dictionary Example 3
This Python map two lists into a Dictionary code is the same as above. However, in this python program, we are allowing the user to insert the keys and values.
# Python Program to Map two lists into a Dictionary keys = [] values = [] num = int(input("Please enter the Number of elements for this Dictionary : ")) print("Integer Values for Keys") for i in range(0, num): x = int(input("Enter Key " + str(i + 1) + " = ")) keys.append(x) print("Integer Values for Values") for i in range(0, num): x = int(input("Enter Value " + str(i + 1) + " = ")) values.append(x) myDict = dict(zip(keys, values)) print("Dictionary Items : ", myDict)