Sick Gaming
[Tut] How to Create a Dictionary from two Lists - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.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

<div><p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">In this article, you’ll learn how to create a <a href="https://blog.finxter.com/python-dictionary/" data-type="post" data-id="5232" target="_blank" rel="noreferrer noopener">Dictionary</a> from two (2) Lists in Python. </p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">To make it more fun, we have the following running scenario:</p>
<p><em><strong>Biogenix</strong>, a Lab Supplies company, needs to determine the best element from the <a href="https://pubchem.ncbi.nlm.nih.gov/periodic-table/" data-type="URL" data-id="https://pubchem.ncbi.nlm.nih.gov/periodic-table/" target="_blank" rel="noreferrer noopener">Periodic Table</a> 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.</em></p>
<p><em>For simplicity, this article uses 10 of the 118 elements in the <a rel="noreferrer noopener" href="https://science.widener.edu/~svanbram/ptable_6.pdf" data-type="URL" data-id="https://science.widener.edu/~svanbram/ptable_6.pdf" target="_blank">Periodic Table</a></em>.</p>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How would we create a Dictionary from two (2) Lists?</p>
<p>We can accomplish this task by one of the following options:</p>
<ul>
<li><strong>Method 1</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank"><code>dict()</code> </a>and <a rel="noreferrer noopener" href="https://blog.finxter.com/zip-unzip-python/" data-type="URL" data-id="https://blog.finxter.com/zip-unzip-python/" target="_blank"><code>zip()</code></a></li>
<li><strong>Method 2</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary-comprehension/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary-comprehension/" target="_blank">Dictionary Comprehension</a></li>
<li><strong>Method 3</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-use-generator-expressions-in-python-dictionaries/" data-type="URL" data-id="https://blog.finxter.com/how-to-use-generator-expressions-in-python-dictionaries/" target="_blank">Generator Expression</a> with <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank"><code>dict()</code> </a>and <a rel="noreferrer noopener" href="https://blog.finxter.com/zip-unzip-python/" data-type="URL" data-id="https://blog.finxter.com/zip-unzip-python/" target="_blank"><code>zip()</code></a></li>
<li><strong>Method 4</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" data-type="URL" data-id="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" target="_blank">Lambda</a></li>
</ul>
<hr class="wp-block-separator"/>
<h2>Method 1: Use dict() and zip()</h2>
<p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://blog.finxter.com/zip-unzip-python/" data-type="URL" data-id="https://blog.finxter.com/zip-unzip-python/" target="_blank"><code>zip()</code></a> to merge two (2) lists into an <a href="https://blog.finxter.com/iterators-iterables-and-itertools/" data-type="post" data-id="29507" target="_blank" rel="noreferrer noopener">iterable</a> object and (<a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank"><code>dict()</code></a>) to convert it into a Dictionary as <em>key:value</em> pairs. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4-5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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 ("{:&lt;20} {:&lt;15}".format(k, v))</pre>
<ul>
<li>Lines [1-2] create two (2) <a href="https://blog.finxter.com/python-lists/" data-type="post" data-id="7332" target="_blank" rel="noreferrer noopener">lists</a> containing the Element Name (<code>el_name</code>) and corresponding Atomic Mass (<code>el_atom</code>), respectively. </li>
<li>Line [3] merges the two (2) lists using <a rel="noreferrer noopener" href="https://blog.finxter.com/zip-unzip-python/" data-type="URL" data-id="https://blog.finxter.com/zip-unzip-python/" target="_blank"><code>zip()</code></a> and converts them into an iterable object. The results save to <code>merge_lists</code>.</li>
<li>Line [4] converts <code>merge_lists</code> into a Dictionary (<a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank"><code>dict())</code></a>. The results save to <code>new_dict</code> as <em>key:value</em> pairs.</li>
<li>Line [5] instantiates a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank">For</a> loop to return the <em>key:value</em> pairs from <code>new_dict</code>.
<ul>
<li>Each iteration outputs the <em>key:value</em> pair in a column format to the terminal.</li>
</ul>
</li>
</ul>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Zip &amp; Unzip: How Does It Work in Python?" width="780" height="439" src="https://www.youtube.com/embed/7zx603xCri4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
<p><strong>Code (snippet)</strong></p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>Meitnerium</td>
<td>277.154</td>
</tr>
<tr>
<td>Darmstadtium </td>
<td>282.166</td>
</tr>
<tr>
<td>Roentgenium </td>
<td>282.169</td>
</tr>
<tr>
<td>Copernicium </td>
<td>286.179</td>
</tr>
<tr>
<td>Nihonium </td>
<td>286.182</td>
</tr>
</tbody>
</table>
</figure>
<hr class="wp-block-separator"/>
<h2>Method 2: Use Dictionary Comprehension</h2>
<p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://blog.finxter.com/zip-unzip-python/" data-type="URL" data-id="https://blog.finxter.com/zip-unzip-python/" target="_blank"></a><a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary-comprehension/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary-comprehension/" target="_blank">Dictionary Comprehension</a> to merge two (2) lists into an iterable object and convert it into a Dictionary as <em>key:value</em> pairs. A great one-liner!</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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 ("{:&lt;20} {:&lt;15}".format(k, v))</pre>
<ul>
<li>Lines [1-2] create two (2) lists containing the Element Name (<code>el_name</code>) and the corresponding Atomic Mass (<code>el_atom</code>), respectively.</li>
<li>Line [3] merges the lists as <em>key:value</em> pairs and converts them into a Dictionary. The results save to <code>new_dict</code>.</li>
<li>Line [4] instantiates a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank">For</a> loop to return the <em>key:value</em> pairs from <code>new_dict</code>.
<ul>
<li>Each iteration outputs the <em>key:value</em> pair in a column format to the terminal.</li>
</ul>
</li>
</ul>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Python Dictionary Comprehension - A Powerful One-Liner Tutorial" width="780" height="439" src="https://www.youtube.com/embed/TlEC5Jx72Uc?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
<p><strong>Code (snippet)</strong></p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>Meitnerium</td>
<td>277.154</td>
</tr>
<tr>
<td>Darmstadtium </td>
<td>282.166</td>
</tr>
<tr>
<td>Roentgenium </td>
<td>282.169</td>
</tr>
<tr>
<td>Copernicium </td>
<td>286.179</td>
</tr>
<tr>
<td>Nihonium </td>
<td>286.182</td>
</tr>
</tbody>
</table>
</figure>
<hr class="wp-block-separator"/>
<h2>Method 3: Use Generator Expression with zip() and dict()</h2>
<p class="has-global-color-8-background-color has-background">This method uses a <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-use-generator-expressions-in-python-dictionaries/" data-type="URL" data-id="https://blog.finxter.com/how-to-use-generator-expressions-in-python-dictionaries/" target="_blank">Generator Expression</a> to merge two (2) lists <br />into an iterable object (<a rel="noreferrer noopener" href="https://blog.finxter.com/zip-unzip-python/" data-type="URL" data-id="https://blog.finxter.com/zip-unzip-python/" target="_blank"><code>zip()</code></a>) and convert it into a Dictionary (<a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank"><code>dict()</code></a>) as <em>key:value</em> pairs.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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 ("{:&lt;20} {:&lt;15}".format(k, v))</pre>
<ul>
<li>Lines [1-2] create two (2) lists containing the Element Name (<code>el_name</code>) and the corresponding Atomic Mass (<code>el_atom</code>) respectively. </li>
<li>Line [3] uses a Generator Expression to merge the lists (<a rel="noreferrer noopener" href="https://blog.finxter.com/zip-unzip-python/" data-type="URL" data-id="https://blog.finxter.com/zip-unzip-python/" target="_blank"><code>zip()</code></a>) and create an iterable object. The object converts into a Dictionary (<a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank"><code>dict()</code></a>) and saves back to <code>gen_exp</code>.</li>
<li>Line [5] instantiates a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank">For</a> loop to return the <em>key:value</em> pairs from <code>new_dict</code>.
<ul>
<li>Each iteration outputs the <em>key:value</em> pair in a column format to the terminal.</li>
</ul>
</li>
</ul>
<p><strong>Code (snippet)</strong></p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>Meitnerium</td>
<td>277.154</td>
</tr>
<tr>
<td>Darmstadtium </td>
<td>282.166</td>
</tr>
<tr>
<td>Roentgenium </td>
<td>282.169</td>
</tr>
<tr>
<td>Copernicium </td>
<td>286.179</td>
</tr>
<tr>
<td>Nihonium </td>
<td>286.182</td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Python One-Liner Trick 8 - Evaluate a Condition in a Dictionary of Dictionaries" width="780" height="439" src="https://www.youtube.com/embed/_M4dlUPG4XI?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
<hr class="wp-block-separator"/>
<h2>Method 4: Use a Lambda</h2>
<p class="has-global-color-8-background-color has-background">This method uses a <a rel="noreferrer noopener" href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" data-type="URL" data-id="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" target="_blank">Lambda</a> to merge two (2) lists into an iterable object (<a rel="noreferrer noopener" href="https://blog.finxter.com/zip-unzip-python/" data-type="URL" data-id="https://blog.finxter.com/zip-unzip-python/" target="_blank"><code>zip()</code></a>) and convert it into a Dictionary (<a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank"><code>dict()</code></a>) as <em>key:value</em> pairs.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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 ("{:&lt;20} {:&lt;15}".format(k, v))</pre>
<ul>
<li>Lines [1-2] create two (2) lists containing the Element Name (<code>el_name</code>) and the corresponding Atomic Mass (<code>el_atom</code>) respectively. </li>
<li>Line [3] uses a <a rel="noreferrer noopener" href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" data-type="URL" data-id="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" target="_blank">lambda</a> to merge the lists (<a rel="noreferrer noopener" href="https://blog.finxter.com/zip-unzip-python/" data-type="URL" data-id="https://blog.finxter.com/zip-unzip-python/" target="_blank"><code>zip()</code></a>) and create an iterable object. The results save to a Dictionary <code>new_dict</code> as key:value pairs.</li>
<li>Line [4] instantiates a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank">For</a> loop to return the <em>key:value</em> pairs from <code>new_dict</code>.
<ul>
<li>Each iteration outputs the <em>key:value</em> pair in a column format to the terminal.</li>
</ul>
</li>
</ul>
<p><strong>Code (snippet)</strong></p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>Meitnerium</td>
<td>277.154</td>
</tr>
<tr>
<td>Darmstadtium </td>
<td>282.166</td>
</tr>
<tr>
<td>Roentgenium </td>
<td>282.169</td>
</tr>
<tr>
<td>Copernicium </td>
<td>286.179</td>
</tr>
<tr>
<td>Nihonium </td>
<td>286.182</td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Let's Play Finxter - The Lambda Function in Python" width="780" height="439" src="https://www.youtube.com/embed/kBg4n52XoUQ?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
<hr class="wp-block-separator"/>
<h2>Summary</h2>
<p>After reviewing the above methods, we decide that Method 2 is best suited: minimal overhead and no additional functions required.</p>
<p>Problem Solved! Happy Coding!</p>
<hr class="wp-block-separator"/>
</div>


https://www.sickgaming.net/blog/2022/04/22/how-to-create-a-dictionary-from-two-lists/