Hurry! Try our new Interactive Courses for FREE. 🥳   🚀
  Signup/Sign In

Convert two lists to dictionary in Python

In this article, we will learn to convert two lists to a dictionary in Python. We will use some built-in functions, simple approaches, and some custom code as well to understand different ways. Let's first have a quick look over what is a list and a dictionary in Python.

Python Lists

Python has a built-in data type called list. It is like a collection of arrays with different methodology. Data inside the list can be of any type say, integer, string or a float value, or even a list type. The list uses comma-separated values within square brackets to store data. Lists can be defined using any variable name and then assigning different values to the list in a square bracket. The list is ordered, changeable, and allows duplicate values.

List Example-

list1 = ["Ram", "Arun", "Kiran"]
list2 = [16, 78, 32, 67]
list3 = ["apple", "mango", 16, "cherry", 3.4]

Python Dictionary

Dictionaries are Python's other built-in data type and it is also known as an associative array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value. Data inside a dictionary can be of any type say, integer, string or a float value, etc.. A dictionary can be defined using any variable name and then assigning different key-value pairs in curly braces.

Dictionary Example-

dict1 = {"A" : 1, "B" : 2, "C" : 3}
dict2 = {"Name": "Jeffery", 1: [2, 4, 3]}
dict3 = {1: "first", 2: "second"}

It sometimes becomes necessary to convert lists into a key-value pair array. Your lists must be same in length. Now, we will discuss and take two lists and mark them together to create a Python dictionary using three different ways.

Convert two lists to a dictionary using loop and remove() function

This is a brute force approach that uses for loop and remove() function to convert two lists to a dictionary. The below example simply declares an empty dictionary, and then run a nested loop for both the lists and assign key and value pairs from list values to the dictionary. While adding key-value pairs to the dictionary, it simultaneously removes the value from list2.

#define two lists
list1 = ["blue", "black", "tangerine", "brown"]
list2 = [23, 44, 51, 14] 

#keys-value lists
print ("Keys: " + str(list1))
print ("Values: " + str(list2)) 
  
#empty dictionary
dict1 = {}

for key in list1:
    for value in list2:
        dict1[key] = value
        list2.remove(value)
        break  

#resultant dictionary 
print ("Resultant dictionary is : " +  str(dict1)) 


Keys: ['blue', 'black', 'tangerine', 'brown']
Values: [23, 44, 51, 14]
Resultant dictionary is : {'blue': 23, 'black': 44, 'tangerine': 51, 'brown': 14}


Convert two lists to a dictionary using dictionary comprehension

This method uses dictionary comprehension to perform the conversion. It is a more concise way to convert two lists to a dictionary because it offers a faster and time-saving approach by reducing the lines to type.

#define two lists
list1 = ["blue", "black", "tangerine", "brown"]
list2 = [23, 44, 51, 14] 

#keys-value lists
print ("Keys: " + str(list1))
print ("Values: " + str(list2))

dict1 = {list1[i]: list2[i] for i in range(len(list1))} 

#resultant dictionary 
print("Resultant dictionary is : " +  str(dict1))


Keys: ['blue', 'black', 'tangerine', 'brown']
Values: [23, 44, 51, 14]
Resultant dictionary is : {'blue': 23, 'black': 44, 'tangerine': 51, 'brown': 14}


Convert two lists to a dictionary using zip() function

The most pythonic, generic, and used method to perform the conversion is by using zip() function. This function pairs the list item with another list item at the corresponding index in the form of key-value pairs. zip() takes two zip iterators as arguments and dict() joins them together into a dictionary. This is known as the most performant approach and the function dict(zip(keys, values)) does require the one-time global lookup each for dict and zip, but it doesn't form any unnecessary intermediate data-structures or have to deal with local lookups in function applications.

#define two lists
list1 = ["blue", "black", "tangerine", "brown"]
list2 = [23, 44, 51, 14]

#keys-value lists
print ("Keys: " + str(list1))
print ("Values: " + str(list2))

dict1 = dict(zip(list1, list2))

  
#resultant dictionary 
print("Resultant dictionary is : " +  str(dict1))


Keys: ['blue', 'black', 'tangerine', 'brown']
Values: [23, 44, 51, 14]
Resultant dictionary is : {'blue': 23, 'black': 44, 'tangerine': 51, 'brown': 14}

Note: In all these methods, if your length of the lists is not equal, the iterator stops when the shortest input iterable is finished.

Conclusion

In this article, we learned to convert two lists to a dictionary by using functions such as remove(), zip(), dict() etc and we used some custom code as well. We must keep in mind to have equal length of lists.