Map Two Lists into a Dictionary in Python



In Python, dictionaries are used to store data as key-value pairs, and lists store a collection of items separated by commas. To map two lists into a dictionary, we have various methods in Python that we will explore in this article.

Mapping two lists into a Dictionary

Mapping two lists into a dictionary can be done using the following methods -

Using a Loop

For loops are used to iterate over the range of items. To map two lists in Python using loops, we need to iterate over the items in the lists and assign them to an empty dictionary (as keys and values, respectively).

Example

In this example, we have created a dictionary by mapping each key from the keys list with the corresponding value from the values list using a for loop.

keys = ['Companyname', 'Type', 'city']
values = ['Tutorialspoint', 'EdTech Limited', 'Hyderabad']

result = {}
for i in range(len(keys)):
   result[keys[i]] = values[i]

print(result)

Output

{'Companyname': 'Tutorialspoint', 'Type': 'EdTech Limited', 'city': 'Hyderabad'}

Using a Zip and Dict Method

In Python, zip() function is used to combine two or more iterables into a tuple. The dict() method is a built-in constructor to create dictionaries. It accepts an iterable containing key-value pairs as a parameter and creates a dictionary based on the given values.

Therefore, to map two lists into a tuple, you need to combine them using the zip() function and pass the resultant tuple to a dict() method to create a dictionary.

Example

In this example using the zip() function, we have combined each item from the keys list with the corresponding item in the values list and then converted it into a dictionary.

keys = ['Companyname', 'Type', 'city']
values = ['Tutorialspoint', 'EdTech Limited', 'Hyderabad']

dicttuple = zip(keys, values)
result = dict(dicttuple)
print(result)

Output

{'Companyname': 'Tutorialspoint', 'Type': 'EdTech Limited', 'city': 'Hyderabad'}

Using Dictionary Comprehension

Using dictionary comprehension, we can create a dictionary by mapping two lists in a single line of code.

Example

In this example, we have created a dictionary by mapping each key from the keys list with the corresponding value from the values list using a dictionary comprehension.

keys = ['Companyname', 'Type', 'city']
values = ['Tutorialspoint', 'EdTech Limited', 'Hyderabad']

result = {keys[i]: values[i] for i in range(len(keys))}
print(result)

Output

{'Companyname': 'Tutorialspoint', 'Type': 'EdTech Limited', 'city': 'Hyderabad'}
Updated on: 2025-05-20T15:27:23+05:30

385 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements