![]() |
|
[Tut] How to Create a Dictionary from two Lists - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Create a Dictionary from two Lists (/thread-99293.html) |
[Tut] How to Create a Dictionary from two Lists - xSicKxBot - 04-27-2022 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 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))
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))
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))
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))
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! |