Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Create a Dictionary from two Lists
1
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.

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

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 (
    Code:
    el_name
    ) and corresponding Atomic Mass (
    Code:
    el_atom
    ), respectively.

  • Line [3] merges the two (2) lists using
    Code:
    zip()
    and converts them into an iterable object. The results save to
    Code:
    merge_lists
    .

  • Line [4] converts
    Code:
    merge_lists
    into a Dictionary (
    Code:
    dict())
    . The results save to
    Code:
    new_dict
    as key:value pairs.

  • 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.



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 (
    Code:
    el_name
    ) and the corresponding Atomic Mass (
    Code:
    el_atom
    ), respectively.

  • 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.



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

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 (
    Code:
    el_name
    ) and the corresponding Atomic Mass (
    Code:
    el_atom
    ) respectively.

  • Line [3] uses a Generator Expression to merge the lists (
    Code:
    zip()
    ) and create an iterable object. The object converts into a Dictionary (
    Code:
    dict()
    ) and saves back to
    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.

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

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 (
    Code:
    el_name
    ) and the corresponding Atomic Mass (
    Code:
    el_atom
    ) respectively.

  • Line [3] uses a lambda to merge the lists (
    Code:
    zip()
    ) and create an iterable object. The results save to a Dictionary
    Code:
    new_dict
    as key:value pairs.

  • 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.

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!
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: