04-27-2022, 05:37 PM
How to Create a Dictionary from two Lists
In this article, you’ll learn how to create a Dictionary from two (2) Lists in Python.
To make it more fun, we have the following running scenario:
Biogenix, a Lab Supplies company, needs to determine the best element from the Periodic Table for producing a new product. They have two (2) lists. The Element Name, the other Atomic Mass. They would prefer this in a Dictionary format.
For simplicity, this article uses 10 of the 118 elements in the Periodic Table.
Question: How would we create a Dictionary from two (2) Lists?
We can accomplish this task by one of the following options:
Method 1: Use dict() and zip()
This method uses to merge two (2) lists into an iterable object and () to convert it into a Dictionary as key:value pairs.
Code (snippet)
Meitnerium |
277.154
Darmstadtium |
282.166
Roentgenium |
282.169
Copernicium |
286.179
Nihonium |
286.182
Method 2: Use Dictionary Comprehension
This method uses https://blog.finxter.com/zip-unzip-python/Dictionary Comprehension to merge two (2) lists into an iterable object and convert it into a Dictionary as key:value pairs. A great one-liner!
Code (snippet)
Meitnerium |
277.154
Darmstadtium |
282.166
Roentgenium |
282.169
Copernicium |
286.179
Nihonium |
286.182
Method 3: Use Generator Expression with zip() and dict()
This method uses a Generator Expression to merge two (2) lists
into an iterable object () and convert it into a Dictionary () as key:value pairs.
Code (snippet)
Meitnerium |
277.154
Darmstadtium |
282.166
Roentgenium |
282.169
Copernicium |
286.179
Nihonium |
286.182
Method 4: Use a Lambda
This method uses a Lambda to merge two (2) lists into an iterable object () and convert it into a Dictionary () as key:value pairs.
Code (snippet)
Meitnerium |
277.154
Darmstadtium |
282.166
Roentgenium |
282.169
Copernicium |
286.179
Nihonium |
286.182
Summary
After reviewing the above methods, we decide that Method 2 is best suited: minimal overhead and no additional functions required.
Problem Solved! Happy Coding!
In this article, you’ll learn how to create a Dictionary from two (2) Lists in Python.
To make it more fun, we have the following running scenario:
Biogenix, a Lab Supplies company, needs to determine the best element from the Periodic Table for producing a new product. They have two (2) lists. The Element Name, the other Atomic Mass. They would prefer this in a Dictionary format.
For simplicity, this article uses 10 of the 118 elements in the Periodic Table.
Question: How would we create a Dictionary from two (2) Lists?We can accomplish this task by one of the following options:
- Method 1: Use andCode:
dict()Code:zip()
- Method 2: Use Dictionary Comprehension
- Method 3: Use Generator Expression with andCode:
dict()Code:zip()
- Method 4: Use Lambda
Method 1: Use dict() and zip()
This method uses
Code:
zip()Code:
dict()Code:
el_name = ['Meitnerium', 'Darmstadtium', 'Roentgenium', 'Copernicium', 'Nihonium', 'Flerovium', 'Moscovium', 'Livermorium', 'Tennessine', 'Oganesson']
el_atom = [277.154, 282.166, 282.169, 286.179, 286.182, 290.192, 290.196, 293.205, 294.211, 295.216] merge_lists = zip(el_name, el_atom)
new_dict = dict(merge_lists) for k, v in new_dict.items(): print ("{:<20} {:<15}".format(k, v))- Lines [1-2] create two (2) lists containing the Element Name () and corresponding Atomic Mass (Code:
el_name), respectively.Code:el_atom
- Line [3] merges the two (2) lists using and converts them into an iterable object. The results save toCode:
zip().Code:merge_lists
- Line [4] converts into a Dictionary (Code:
merge_lists. The results save toCode:dict())as key:value pairs.Code:new_dict
- Line [5] instantiates a For loop to return the key:value pairs from .Code:
new_dict- Each iteration outputs the key:value pair in a column format to the terminal.
- Each iteration outputs the key:value pair in a column format to the terminal.
Code (snippet)
Meitnerium |
277.154
Darmstadtium |
282.166
Roentgenium |
282.169
Copernicium |
286.179
Nihonium |
286.182
Method 2: Use Dictionary Comprehension
This method uses https://blog.finxter.com/zip-unzip-python/Dictionary Comprehension to merge two (2) lists into an iterable object and convert it into a Dictionary as key:value pairs. A great one-liner!
Code:
el_name = ['Meitnerium', 'Darmstadtium', 'Roentgenium', 'Copernicium', 'Nihonium', 'Flerovium', 'Moscovium', 'Livermorium', 'Tennessine', 'Oganesson']
el_atom = [277.154, 282.166, 282.169, 286.179, 286.182, 290.192, 290.196, 293.205, 294.211, 295.216]
new_dict = {el_name[i]: el_atom[i] for i in range(len(el_name))} for k, v in new_dict.items(): print ("{:<20} {:<15}".format(k, v))- Lines [1-2] create two (2) lists containing the Element Name () and the corresponding Atomic Mass (Code:
el_name), respectively.Code:el_atom
- Line [3] merges the lists as key:value pairs and converts them into a Dictionary. The results save to .Code:
new_dict
- Line [4] instantiates a For loop to return the key:value pairs from .Code:
new_dict- Each iteration outputs the key:value pair in a column format to the terminal.
- Each iteration outputs the key:value pair in a column format to the terminal.
Code (snippet)
Meitnerium |
277.154
Darmstadtium |
282.166
Roentgenium |
282.169
Copernicium |
286.179
Nihonium |
286.182
Method 3: Use Generator Expression with zip() and dict()
This method uses a Generator Expression to merge two (2) lists
into an iterable object (
Code:
zip()Code:
dict()Code:
el_name = ['Meitnerium', 'Darmstadtium', 'Roentgenium', 'Copernicium', 'Nihonium', 'Flerovium', 'Moscovium', 'Livermorium', 'Tennessine', 'Oganesson']
el_atom = [277.154, 282.166, 282.169, 286.179, 286.182, 290.192, 290.196, 293.205, 294.211, 295.216]
gen_exp = dict(((k, v) for k, v in zip(el_name, el_atom))) for k, v in new_dict.items(): print ("{:<20} {:<15}".format(k, v))- Lines [1-2] create two (2) lists containing the Element Name () and the corresponding Atomic Mass (Code:
el_name) respectively.Code:el_atom
- Line [3] uses a Generator Expression to merge the lists () and create an iterable object. The object converts into a Dictionary (Code:
zip()) and saves back toCode:dict().Code:gen_exp
- Line [5] instantiates a For loop to return the key:value pairs from .Code:
new_dict- Each iteration outputs the key:value pair in a column format to the terminal.
- Each iteration outputs the key:value pair in a column format to the terminal.
Code (snippet)
Meitnerium |
277.154
Darmstadtium |
282.166
Roentgenium |
282.169
Copernicium |
286.179
Nihonium |
286.182
Method 4: Use a Lambda
This method uses a Lambda to merge two (2) lists into an iterable object (
Code:
zip()Code:
dict()Code:
el_name = ['Meitnerium', 'Darmstadtium', 'Roentgenium', 'Copernicium', 'Nihonium', 'Flerovium', 'Moscovium', 'Livermorium', 'Tennessine', 'Oganesson']
el_atom = [277.154, 282.166, 282.169, 286.179, 286.182, 290.192, 290.196, 293.205, 294.211, 295.216]
new_dict = dict((lambda n, a: {name: el_atom for name, el_atom in zip(n, a)})(el_name, el_atom)) for k, v in new_dict.items(): print ("{:<20} {:<15}".format(k, v))- Lines [1-2] create two (2) lists containing the Element Name () and the corresponding Atomic Mass (Code:
el_name) respectively.Code:el_atom
- Line [3] uses a lambda to merge the lists () and create an iterable object. The results save to a DictionaryCode:
zip()as key:value pairs.Code:new_dict
- Line [4] instantiates a For loop to return the key:value pairs from .Code:
new_dict- Each iteration outputs the key:value pair in a column format to the terminal.
- Each iteration outputs the key:value pair in a column format to the terminal.
Code (snippet)
Meitnerium |
277.154
Darmstadtium |
282.166
Roentgenium |
282.169
Copernicium |
286.179
Nihonium |
286.182
Summary
After reviewing the above methods, we decide that Method 2 is best suited: minimal overhead and no additional functions required.
Problem Solved! Happy Coding!

