How to Create a Dictionary from two Lists

Rate this post

Problem Formulation and Solution Overview

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 zip() to merge two (2) lists into an iterable object and (dict()) to convert it into a Dictionary as key:value pairs.

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 (el_name) and corresponding Atomic Mass (el_atom), respectively.
  • Line [3] merges the two (2) lists using zip() and converts them into an iterable object. The results save to merge_lists.
  • Line [4] converts merge_lists into a Dictionary (dict()). The results save to new_dict as key:value pairs.
  • Line [5] instantiates a For loop to return the key:value pairs from new_dict.
    • Each iteration outputs the key:value pair in a column format to the terminal.

Code (snippet)

Meitnerium277.154
Darmstadtium 282.166
Roentgenium 282.169
Copernicium 286.179
Nihonium 286.182

Method 2: Use Dictionary Comprehension

This method uses 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!

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 (el_name) and the corresponding Atomic Mass (el_atom), respectively.
  • Line [3] merges the lists as key:value pairs and converts them into a Dictionary. The results save to new_dict.
  • Line [4] instantiates a For loop to return the key:value pairs from new_dict.
    • Each iteration outputs the key:value pair in a column format to the terminal.

Code (snippet)

Meitnerium277.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 (zip()) and convert it into a Dictionary (dict()) as key:value pairs.

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 (el_name) and the corresponding Atomic Mass (el_atom) respectively.
  • Line [3] uses a Generator Expression to merge the lists (zip()) and create an iterable object. The object converts into a Dictionary (dict()) and saves back to gen_exp.
  • Line [5] instantiates a For loop to return the key:value pairs from new_dict.
    • Each iteration outputs the key:value pair in a column format to the terminal.

Code (snippet)

Meitnerium277.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 (zip()) and convert it into a Dictionary (dict()) as key:value pairs.

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 (el_name) and the corresponding Atomic Mass (el_atom) respectively.
  • Line [3] uses a lambda to merge the lists (zip()) and create an iterable object. The results save to a Dictionary new_dict as key:value pairs.
  • Line [4] instantiates a For loop to return the key:value pairs from new_dict.
    • Each iteration outputs the key:value pair in a column format to the terminal.

Code (snippet)

Meitnerium277.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!