In this example we will show how to map two lists to a dictionary in Python.
Source Code
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
def userList(n):
a = []
for i in range(n):
j = int(input("Please input the " + str(i + 1) + "th element: "))
a.append(j)
return a
n = int(input("Please input the number of elements: "))
print("Key: ")
keys = userList(n)
print("Value: ")
values = userList(n)
dict1 = dict(zip(keys,values))
print("The dictionary is: ", dict1)
Output:
Please input the number of elements: 4
Key:
Please input the 1th element: 1
Please input the 2th element: 2
Please input the 3th element: 3
Please input the 4th element: 4
Value:
Please input the 1th element: 1
Please input the 2th element: 4
Please input the 3th element: 9
Please input the 4th element: 16
The dictionary is: {1: 1, 2: 4, 3: 9, 4: 16}