How to make a dictionary from two lists of keys and values in python

To make a dictionary from two lists in python use the zip() and dict() functionszip() and dict() functions are built-in python functions that help to build a dictionary from two separate lists in python.

zip() function in Python

The zip() function is a Python built-in function that iterates over several iterables in parallel and produces a tuple. zip() the function can help us iterate over the lists very easily and gives us a predictable output.

Example of zip() function


digit_list = [1,2,3]
figure_list= ['one', 'two', 'three']
for c_p in zip(digit_list,figure_list):
    print(c_p)

# Output
# (1, 'one')
# (2, 'two')
# (3, 'three')

dict() funciton in Python

The dict() function is a Python built-in function that creates a new python dictionary. The dict() function in python can take an iterable or a mapping object as an argument and return a dictionary object.

Example of dict() function

Make a dictionary from two lists of keys and values in python

To make a dictionary from two lists, first, use the zip() function to convert the lists into the list tuples, and then use the dict() function to make a dictionary from the list of tuples.Steps to build a dictionary from two lists

  1.  Convert the two lists into a list of tuplesYou can use the zip() function to convert the two given lists into a tuple of pair values. The tuple will contain the key and value of the dictionary and it will be just in the tuple form.
  2.  Convert the tuples into a dictionaryUse the dict() function to convert the list of tuples into a dictionary in python. All you have to do is to pass the list of tuples into the dict() function and it will return the dictionary from the lists of keys and values.

Python code to make a dictionary from two lists of keys and values in python

digit_list = [1,2,3]
figure_list= ['one', 'two', 'three']
list_of_tuples= zip(digit_list,figure_list)
dictionary = dict(list_of_tuples)
print(dictionary)

# output 
# {1: 'one', 2: 'two', 3: 'three'} 

Leave a Comment

Scroll to Top