Want to convert two lists into a dictionary? Do the following:
- Zip the lists together using
zip(list_1, list_2)
. - Create a dictionary from the list of tuples using the constructor
dict()
on the result. - In other words, call
dict(zip(list_1, list_2))
to convert two lists into a dictionary.
Try it yourself:
Let’s dive into the code step-by-step.
Problem: Given are two equal-sized lists. How can you convert them into a dictionary by using the elements of the first list as keys and the elements of the second list as values?
Example: Say, you’ve got a list of persons ['Alice', 'Bob', 'Ann']
and a list of ages [18, 21, 33]
. You want to associated the persons to their ages so that the first person in the first list is associated to the first age value in the second list, the second person to the second age value, and so on. Ultimately, you want to obtain the dictionary {'Alice': 18, 'Bob': 21, 'Ann': 33}.
Naive solution: Given are two lists list_1
and list_2
.
- Create an empty dictionary
dic
. - In a for loop, iterate over each index
i
between0
andlen(list_1)
—the latter being excluded. - For each index
i
use the result oflist_1[i]
as a key and the result oflist_2[i]
as a value of the dictionary. In other words, your loop body executesdic[list_1[i]] = list_2[i]
.
Here’s the naive code that solves the problem:
list_1 = ['Alice', 'Bob', 'Ann'] list_2 = [18, 21, 33] dic = {} for i in range(0,len(list_1)): dic[list_1[i]] = list_2[i] print(dic) # {'Alice': 18, 'Bob': 21, 'Ann': 33}
After creating the two lists, you create the empty dictionary dic. Then you iterate over all integer values 0, 1, 2 using the len()
function on the first list. As both lists have the same length, this is not a problem. See my article on the len()
function if you want to know how it works inside out. You then use the i-th value of the first list as a dictionary key and the i-th value of the second list as a dictionary value.
Pythonic One-Liner Solution: However, a better and more concise way of accomplishing the same thing in a more effective way is to use the zip function to combine both lists into a new iterable where the i-th value of list_1
is associated with the i-th value of list_2
in a new tuple.
Here’s the code comparing the naive solution you’ve already seen to the more concise, faster, and more Pythonic solution based on the zip() function:
list_1 = ['Alice', 'Bob', 'Ann'] list_2 = [18, 21, 33] # Naive Solution dic = {} for i in range(0,len(list_1)): dic[list_1[i]] = list_2[i] # Python One-Liner Solution dic = dict(zip(list_1, list_2)) print(dic) # {'Alice': 18, 'Bob': 21, 'Ann': 33}
The zip function takes a number of
(An iter-able is an object that contains multiple elements over which you can iterate. Examples are lists, sets, or tuples.)
Say, you have two lists:
[1,2,3] [4,5,6]
Now you zip them together and get the new list:
[(1,4), (2,5), (3,6)]
Now, you know how to combine two lists into a dictionary. Watch my One-Liner explainer video that I created for the launch of my new book “Python One-Liners”.
Here’s the webinar link towards creating your six-figure coding business. Check it out!