Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Convert two lists into a dictionary

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 30 Dec, 2022
Improve Article
Save Article

Interconversion between data types are usually necessary in real-time applications as certain systems have certain modules which require the input in a particular data-type. Let’s discuss a simple yet useful utility of conversion of two lists into a key:value pair dictionary. 

Method #1: Naive Method The basic method that can be applied to perform this task is the brute force method to achieve this. For this, simply declare a dictionary, and then run nested loop for both the lists and assign key and value pairs from list values to dictionary. 

Python3




# Python3 code to demonstrate
# conversion of lists to dictionary
# using naive method
 
# initializing lists
test_keys = ["Rash", "Kil", "Varsha"]
test_values = [1, 4, 5]
 
# Printing original keys-value lists
print("Original key list is : " + str(test_keys))
print("Original value list is : " + str(test_values))
 
# using naive method
# to convert lists to dictionary
res = {}
for key in test_keys:
    for value in test_values:
        res[key] = value
        test_values.remove(value)
        break
 
# Printing resultant dictionary
print("Resultant dictionary is : " + str(res))

Output:

Original key list is : ['Rash', 'Kil', 'Varsha']
Original value list is : [1, 4, 5]
Resultant dictionary is : {'Varsha': 5, 'Rash': 1, 'Kil': 4}

Method #2: Using dictionary comprehension The more concise way to achieve the above method, dictionary comprehension method offers a faster and time-saving approach by reducing the lines to type. 

Python3




# Python3 code to demonstrate
# conversion of lists to dictionary
# using dictionary comprehension
 
# initializing lists
test_keys = ["Rash", "Kil", "Varsha"]
test_values = [1, 4, 5]
 
# Printing original keys-value lists
print("Original key list is : " + str(test_keys))
print("Original value list is : " + str(test_values))
 
# using dictionary comprehension
# to convert lists to dictionary
res = {test_keys[i]: test_values[i] for i in range(len(test_keys))}
 
# Printing resultant dictionary
print("Resultant dictionary is : " + str(res))

Output:

Original key list is : ['Rash', 'Kil', 'Varsha']
Original value list is : [1, 4, 5]
Resultant dictionary is : {'Varsha': 5, 'Kil': 4, 'Rash': 1}

Method #3: Using zip() Most pythonic and generic method to perform this very task is by using zip(). This function pairs the list element with other list elements at the corresponding index in form of key-value pairs. 

Python3




# Python3 code to demonstrate
# conversion of lists to dictionary
# using zip()
 
# initializing lists
test_keys = ["Rash", "Kil", "Varsha"]
test_values = [1, 4, 5]
 
# Printing original keys-value lists
print("Original key list is : " + str(test_keys))
print("Original value list is : " + str(test_values))
 
# using zip()
# to convert lists to dictionary
res = dict(zip(test_keys, test_values))
 
# Printing resultant dictionary
print("Resultant dictionary is : " + str(res))

Output:

Original key list is : ['Rash', 'Kil', 'Varsha']
Original value list is : [1, 4, 5]
Resultant dictionary is : {'Kil': 4, 'Rash': 1, 'Varsha': 5}

Method #4: Using map(). We will use this function is used to pair the list element with other list elements at the corresponding index in form of key-value pairs. And type cast this tuple list to the dictionary.

Python




# Python3 code to demonstrate
# conversion of lists to dictionary
# using dict() + map()
 
# initializing lists
keys = ["Rash", "Kil", "Varsha"]
values = [1, 4, 5]
 
# Printing original keys-value lists
print ("Original key list is : " + str(keys))
print ("Original value list is : " + str(values))
 
# using map and dict type casting
# to convert lists to dictionary
res = dict(map(lambda i,j : (i,j) , keys,values))
 
# Printing resultant dictionary
print ("Resultant dictionary is : " + str(res))

Output:

Original key list is : ['Rash', 'Kil', 'Varsha']
Original value list is : [1, 4, 5]
Resultant dictionary is : {'Rash': 1, 'Kil': 4, 'Varsha': 5}

Method #5: Using enumerate()

In this example, the enumerate() function is used to loop over the elements in the zip(test_keys, test_values) object, which pairs the elements in the two lists together. The resulting tuples are then added to a list, which is passed as an argument to the dict() function to create the dictionary.

Python3




# initializing lists
test_keys = ["Rash", "Kil", "Varsha"]
test_values = [1, 4, 5]
 
# create a list of tuples using enumerate()
tuples = [(key, value) for i, (key, value) in enumerate(zip(test_keys, test_values))]
 
# convert list of tuples to dictionary using dict()
res = dict(tuples)
 
print(res)  # {'Rash': 1, 'Kil': 4, 'Varsha': 5}

Output

{'Rash': 1, 'Kil': 4, 'Varsha': 5}

The auxiliary space required by the enumerate approach to convert two lists into a dictionary is O(n), where n is the length of the lists. This is because the approach involves creating a list of tuples, which has a length equal to the length of the lists. In addition, the approach requires space to store the variables key and value in each iteration of the loop.

The time complexity of the enumerate approach is also O(n), as the algorithm needs to iterate over the elements in the lists and perform some operations on each element.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!