07-15-2022, 12:22 AM
How to Multiply List Elements by a Number – Top 5 Ways
<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{"align":"left","id":"467518","slug":"default","valign":"top","reference":"auto","class":"","count":"1","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Problem Formulation and Solution Overview</h2>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">In this article, you’ll learn <strong><em>how to multiply List Elements by a Number in Python</em></strong>.</p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio"><em>This example multiples the first five (5) <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Prime_number" data-type="URL" data-id="https://en.wikipedia.org/wiki/Prime_number" target="_blank">Prime Numbers</a></em> <em>by two (2) and return the result.</em></p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background"><em><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How would we write Python code to multiply the list elements?</em></p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">We can accomplish this task by one of the following options:</p>
<ul type="video" class="wp-embed-aspect-16-9 wp-has-aspect-ratio">
<li><strong>Method 1</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank"><code>List Comprehension</code></a></li>
<li><strong>Method 2</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/pandas-quickstart/" data-type="URL" data-id="https://blog.finxter.com/pandas-quickstart/" target="_blank"><code>Pandas</code></a> <a rel="noreferrer noopener" href="https://pandas.pydata.org/docs/reference/api/pandas.Series.tolist.html" data-type="URL" data-id="https://pandas.pydata.org/docs/reference/api/pandas.Series.tolist.html" target="_blank"><code>tolist()</code></a></li>
<li><strong>Method 3</strong>: Use <a href="https://blog.finxter.com/python-map/"><code>map()</code></a> and 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"><code>lambda()</code></a></li>
<li><strong>Method 4</strong>: Use <a href="https://blog.finxter.com/numpy-tutorial/" data-type="URL" data-id="https://blog.finxter.com/numpy-tutorial/" target="_blank" rel="noreferrer noopener">NumPy</a> <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-initialize-a-numpy-array-6-easy-ways/" data-type="URL" data-id="https://blog.finxter.com/how-to-initialize-a-numpy-array-6-easy-ways/" target="_blank"><code>Array()</code></a></li>
<li><strong>Method 5</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-slicing-in-python/" data-type="URL" data-id="https://blog.finxter.com/introduction-to-slicing-in-python/" target="_blank"><code>Slicing</code></a></li>
</ul>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 1: Use List Comprehension </h2>
<p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank"><code>List Comprehension</code></a> to apply a mathematical operation to each element and return the result.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">prime_nums = [2, 3, 5, 7, 11]
mult_result = [x * 2 for x in prime_nums]
print(mult_result)</pre>
<p>Above declares the first (5) <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Prime_number" data-type="URL" data-id="https://en.wikipedia.org/wiki/Prime_number" target="_blank">Prime Numbers</a> and saves this <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> to <code>prime_nums</code>. Next, <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank"><code>List Comprehension</code></a> loops through each element and applies the multiplication operation to each. The output saves to <code>mult_result</code> and is output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>[4, 6, 10, 14, 22]</code></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 9 - Nested List Comprehension" width="780" height="439" src="https://www.youtube.com/embed/aBC0VhpXkOQ?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 2: Use Pandas tolist()</h2>
<p class="has-global-color-8-background-color has-background">This method requires an additional library to be imported, <a rel="noreferrer noopener" href="https://blog.finxter.com/pandas-quickstart/" data-type="URL" data-id="https://blog.finxter.com/pandas-quickstart/" target="_blank"><code>Pandas</code></a>, to use the <a rel="noreferrer noopener" href="https://pandas.pydata.org/docs/reference/api/pandas.Series.tolist.html" data-type="URL" data-id="https://pandas.pydata.org/docs/reference/api/pandas.Series.tolist.html" target="_blank"><code>tolist()</code></a> function. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1,4-5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd prime_nums = [2, 3, 5, 7, 11]
mult_result = pd.Series(prime_nums)
mult_result = (mult_result*2).tolist()
print(mult_result)</pre>
<p>Above, imports the <a rel="noreferrer noopener" href="https://blog.finxter.com/pandas-quickstart/" data-type="URL" data-id="https://blog.finxter.com/pandas-quickstart/" target="_blank"><code>Pandas</code></a> Library. Click <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-install-pandas-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-install-pandas-in-python/" target="_blank">here</a> if this requires installation. Then, the first (5) <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Prime_number" data-type="URL" data-id="https://en.wikipedia.org/wiki/Prime_number" target="_blank">Prime Numbers</a> are declared and saved to <code>prime_nums</code>.</p>
<p>Next, <code>prime_nums</code> is passed as an argument to the <a rel="noreferrer noopener" href="https://pandas.pydata.org/docs/reference/api/pandas.Series.html" data-type="URL" data-id="https://pandas.pydata.org/docs/reference/api/pandas.Series.html" target="_blank"><code>pd.Series()</code></a> function and returns <code>mult_result</code>. The output of <code>mult_result</code> at this point is shown below. </p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>0 2<br />1 3<br />2 5<br />3 7<br />4 11<br />dtype: int64</code></td>
</tr>
</tbody>
</table>
</figure>
<p>Now, we need to <a href="https://blog.finxter.com/dataframe-to-a-list-and-vice-versa/" data-type="post" data-id="111548" target="_blank" rel="noreferrer noopener">convert</a> this output to a list (<code>tolist()</code>) and apply the multiplication operation to each element. The results save to <code>mult_result</code> and are output to the terminal. </p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>[4, 6, 10, 14, 22]</code></td>
</tr>
</tbody>
</table>
</figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 3: Use map and lambda Functions</h2>
<p class="has-global-color-8-background-color has-background"> This method wraps the <a href="https://blog.finxter.com/python-map/"><code>map()</code></a>, and <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"><code>lambda</code></a> functions inside a Python <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> and calculates the results.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">prime_nums = [2, 3, 5, 7, 11]
mult_result = list(map(lambda x: x*2, prime_nums))
print(mult_result)</pre>
<p>Above declares the first (5) <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Prime_number" data-type="URL" data-id="https://en.wikipedia.org/wiki/Prime_number" target="_blank">Prime Numbers</a> and saves them to <code>prime_nums</code>. The next line does the following:</p>
<ul>
<li>The <a href="https://blog.finxter.com/python-map/"><code>map()</code></a> function is passed the <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"><code>lambda()</code></a> function as an argument <code>(map(lambda x: x*2, prime_nums)</code>).</li>
<li>The <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"><code>lambda</code></a> performs the multiplication operation to each element of <code>prime_nums</code> and saves it to <a href="https://blog.finxter.com/python-map/"><code>map()</code></a> as an object similar to below.<br /><code><map object at 0x000001DC99CBBBB0></code></li>
<li>The <a href="https://blog.finxter.com/python-map/"><code>map()</code></a> object is then converted to a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a>.</li>
<li>The results save to <code>mult_result</code>.</li>
</ul>
<p>Then, <code>mult_result</code> is output to the terminal. </p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>[4, 6, 10, 14, 22]</code></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 has-alpha-channel-opacity" />
<h2>Method 4: Use Numpy Array()</h2>
<p class="has-global-color-8-background-color has-background">This method requires an additional library to be imported, <a rel="noreferrer noopener" href="https://blog.finxter.com/numpy-tutorial/" data-type="URL" data-id="https://blog.finxter.com/numpy-tutorial/" target="_blank">NumPy</a>, to use the <code><a href="https://numpy.org/doc/stable/reference/generated/numpy.array.html" data-type="URL" data-id="https://numpy.org/doc/stable/reference/generated/numpy.array.html" target="_blank" rel="noreferrer noopener">np.array()</a></code> function. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np prime_nums = [2, 3, 5, 7, 11]
the_result = list(np.array(prime_nums) * 2)
print(the_result)</pre>
<p>Above, imports the <a rel="noreferrer noopener" href="https://blog.finxter.com/numpy-tutorial/" data-type="URL" data-id="https://blog.finxter.com/numpy-tutorial/" target="_blank">NumPy</a> Library. Click <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-install-numpy-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-install-numpy-in-python/" target="_blank">here</a> if this requires installation. Then the first (5) <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Prime_number" data-type="URL" data-id="https://en.wikipedia.org/wiki/Prime_number" target="_blank">Prime Numbers</a> are declared and saved to <code>prime_nums</code>.</p>
<p>Next, <code>prime_nums</code> is passed as an argument to <code>np.array()</code> where the multiplication operation is applied to each element. Then, this is converted to a List, saved to <code>the_result</code> and output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>[4, 6, 10, 14, 22]</code></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="NumPy Tutorial - Everything You Need to Know to Get Started" width="780" height="439" src="https://www.youtube.com/embed/s55BEAJRTOk?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 5: Use Slicing</h2>
<p class="has-global-color-8-background-color has-background">This method uses Python’s infamous <a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-slicing-in-python/" data-type="URL" data-id="https://blog.finxter.com/introduction-to-slicing-in-python/" target="_blank"><code>Slicing</code></a>! No overhead, and a very pythonic way to resolve the issue.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">prime_nums = [2, 3, 5, 7, 11]
prime_nums[:] = [x * 2 for x in prime_nums]
print(prime_nums)</pre>
<p>Above declares the first (5) <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Prime_number" target="_blank">Prime Numbers</a> and saves them to <code>prime_nums</code>.</p>
<p>Then slicing is applied and used in conjunction with <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank"><code>List Comprehension</code></a> to apply the multiplication operation to each element. The results save back to <code>prime_nums</code> and are output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>[4, 6, 10, 14, 22]</code></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="The Ultimate Guide to Slicing in Python" width="780" height="439" src="https://www.youtube.com/embed/D2ZueuWXST8?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
<p class="has-text-align-center has-base-3-color has-accent-background-color has-text-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f31f.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />A Finxter Favorite!</p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Summary</h2>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">These methods of multiplying list elements by a number should give you enough information to select the best one for your coding requirements.</p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Good Luck & Happy Coding!</p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Programmer Humor</h2>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f471-200d-2640-fe0f.png" alt="?♀️" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Programmer 1</strong>: We have a problem<br /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f9d4-200d-2642-fe0f.png" alt="?♂️" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Programmer 2</strong>: Let’s use RegEx!<br /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f471-200d-2640-fe0f.png" alt="?♀️" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Programmer 1</strong>: Now we have two problems</p>
<p>… yet – you can easily reduce the two problems to zero as you polish your “<a rel="noreferrer noopener" href="https://blog.finxter.com/python-regex/" data-type="post" data-id="6210" target="_blank">RegEx Superpower in Python</a>“. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
</p></p>
</div>
https://www.sickgaming.net/blog/2022/07/...op-5-ways/
<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{"align":"left","id":"467518","slug":"default","valign":"top","reference":"auto","class":"","count":"1","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Problem Formulation and Solution Overview</h2>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">In this article, you’ll learn <strong><em>how to multiply List Elements by a Number in Python</em></strong>.</p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio"><em>This example multiples the first five (5) <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Prime_number" data-type="URL" data-id="https://en.wikipedia.org/wiki/Prime_number" target="_blank">Prime Numbers</a></em> <em>by two (2) and return the result.</em></p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background"><em><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How would we write Python code to multiply the list elements?</em></p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">We can accomplish this task by one of the following options:</p>
<ul type="video" class="wp-embed-aspect-16-9 wp-has-aspect-ratio">
<li><strong>Method 1</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank"><code>List Comprehension</code></a></li>
<li><strong>Method 2</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/pandas-quickstart/" data-type="URL" data-id="https://blog.finxter.com/pandas-quickstart/" target="_blank"><code>Pandas</code></a> <a rel="noreferrer noopener" href="https://pandas.pydata.org/docs/reference/api/pandas.Series.tolist.html" data-type="URL" data-id="https://pandas.pydata.org/docs/reference/api/pandas.Series.tolist.html" target="_blank"><code>tolist()</code></a></li>
<li><strong>Method 3</strong>: Use <a href="https://blog.finxter.com/python-map/"><code>map()</code></a> and 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"><code>lambda()</code></a></li>
<li><strong>Method 4</strong>: Use <a href="https://blog.finxter.com/numpy-tutorial/" data-type="URL" data-id="https://blog.finxter.com/numpy-tutorial/" target="_blank" rel="noreferrer noopener">NumPy</a> <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-initialize-a-numpy-array-6-easy-ways/" data-type="URL" data-id="https://blog.finxter.com/how-to-initialize-a-numpy-array-6-easy-ways/" target="_blank"><code>Array()</code></a></li>
<li><strong>Method 5</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-slicing-in-python/" data-type="URL" data-id="https://blog.finxter.com/introduction-to-slicing-in-python/" target="_blank"><code>Slicing</code></a></li>
</ul>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 1: Use List Comprehension </h2>
<p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank"><code>List Comprehension</code></a> to apply a mathematical operation to each element and return the result.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">prime_nums = [2, 3, 5, 7, 11]
mult_result = [x * 2 for x in prime_nums]
print(mult_result)</pre>
<p>Above declares the first (5) <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Prime_number" data-type="URL" data-id="https://en.wikipedia.org/wiki/Prime_number" target="_blank">Prime Numbers</a> and saves this <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> to <code>prime_nums</code>. Next, <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank"><code>List Comprehension</code></a> loops through each element and applies the multiplication operation to each. The output saves to <code>mult_result</code> and is output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>[4, 6, 10, 14, 22]</code></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 9 - Nested List Comprehension" width="780" height="439" src="https://www.youtube.com/embed/aBC0VhpXkOQ?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 2: Use Pandas tolist()</h2>
<p class="has-global-color-8-background-color has-background">This method requires an additional library to be imported, <a rel="noreferrer noopener" href="https://blog.finxter.com/pandas-quickstart/" data-type="URL" data-id="https://blog.finxter.com/pandas-quickstart/" target="_blank"><code>Pandas</code></a>, to use the <a rel="noreferrer noopener" href="https://pandas.pydata.org/docs/reference/api/pandas.Series.tolist.html" data-type="URL" data-id="https://pandas.pydata.org/docs/reference/api/pandas.Series.tolist.html" target="_blank"><code>tolist()</code></a> function. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1,4-5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd prime_nums = [2, 3, 5, 7, 11]
mult_result = pd.Series(prime_nums)
mult_result = (mult_result*2).tolist()
print(mult_result)</pre>
<p>Above, imports the <a rel="noreferrer noopener" href="https://blog.finxter.com/pandas-quickstart/" data-type="URL" data-id="https://blog.finxter.com/pandas-quickstart/" target="_blank"><code>Pandas</code></a> Library. Click <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-install-pandas-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-install-pandas-in-python/" target="_blank">here</a> if this requires installation. Then, the first (5) <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Prime_number" data-type="URL" data-id="https://en.wikipedia.org/wiki/Prime_number" target="_blank">Prime Numbers</a> are declared and saved to <code>prime_nums</code>.</p>
<p>Next, <code>prime_nums</code> is passed as an argument to the <a rel="noreferrer noopener" href="https://pandas.pydata.org/docs/reference/api/pandas.Series.html" data-type="URL" data-id="https://pandas.pydata.org/docs/reference/api/pandas.Series.html" target="_blank"><code>pd.Series()</code></a> function and returns <code>mult_result</code>. The output of <code>mult_result</code> at this point is shown below. </p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>0 2<br />1 3<br />2 5<br />3 7<br />4 11<br />dtype: int64</code></td>
</tr>
</tbody>
</table>
</figure>
<p>Now, we need to <a href="https://blog.finxter.com/dataframe-to-a-list-and-vice-versa/" data-type="post" data-id="111548" target="_blank" rel="noreferrer noopener">convert</a> this output to a list (<code>tolist()</code>) and apply the multiplication operation to each element. The results save to <code>mult_result</code> and are output to the terminal. </p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>[4, 6, 10, 14, 22]</code></td>
</tr>
</tbody>
</table>
</figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 3: Use map and lambda Functions</h2>
<p class="has-global-color-8-background-color has-background"> This method wraps the <a href="https://blog.finxter.com/python-map/"><code>map()</code></a>, and <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"><code>lambda</code></a> functions inside a Python <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> and calculates the results.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">prime_nums = [2, 3, 5, 7, 11]
mult_result = list(map(lambda x: x*2, prime_nums))
print(mult_result)</pre>
<p>Above declares the first (5) <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Prime_number" data-type="URL" data-id="https://en.wikipedia.org/wiki/Prime_number" target="_blank">Prime Numbers</a> and saves them to <code>prime_nums</code>. The next line does the following:</p>
<ul>
<li>The <a href="https://blog.finxter.com/python-map/"><code>map()</code></a> function is passed the <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"><code>lambda()</code></a> function as an argument <code>(map(lambda x: x*2, prime_nums)</code>).</li>
<li>The <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"><code>lambda</code></a> performs the multiplication operation to each element of <code>prime_nums</code> and saves it to <a href="https://blog.finxter.com/python-map/"><code>map()</code></a> as an object similar to below.<br /><code><map object at 0x000001DC99CBBBB0></code></li>
<li>The <a href="https://blog.finxter.com/python-map/"><code>map()</code></a> object is then converted to a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a>.</li>
<li>The results save to <code>mult_result</code>.</li>
</ul>
<p>Then, <code>mult_result</code> is output to the terminal. </p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>[4, 6, 10, 14, 22]</code></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 has-alpha-channel-opacity" />
<h2>Method 4: Use Numpy Array()</h2>
<p class="has-global-color-8-background-color has-background">This method requires an additional library to be imported, <a rel="noreferrer noopener" href="https://blog.finxter.com/numpy-tutorial/" data-type="URL" data-id="https://blog.finxter.com/numpy-tutorial/" target="_blank">NumPy</a>, to use the <code><a href="https://numpy.org/doc/stable/reference/generated/numpy.array.html" data-type="URL" data-id="https://numpy.org/doc/stable/reference/generated/numpy.array.html" target="_blank" rel="noreferrer noopener">np.array()</a></code> function. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np prime_nums = [2, 3, 5, 7, 11]
the_result = list(np.array(prime_nums) * 2)
print(the_result)</pre>
<p>Above, imports the <a rel="noreferrer noopener" href="https://blog.finxter.com/numpy-tutorial/" data-type="URL" data-id="https://blog.finxter.com/numpy-tutorial/" target="_blank">NumPy</a> Library. Click <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-install-numpy-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-install-numpy-in-python/" target="_blank">here</a> if this requires installation. Then the first (5) <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Prime_number" data-type="URL" data-id="https://en.wikipedia.org/wiki/Prime_number" target="_blank">Prime Numbers</a> are declared and saved to <code>prime_nums</code>.</p>
<p>Next, <code>prime_nums</code> is passed as an argument to <code>np.array()</code> where the multiplication operation is applied to each element. Then, this is converted to a List, saved to <code>the_result</code> and output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>[4, 6, 10, 14, 22]</code></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="NumPy Tutorial - Everything You Need to Know to Get Started" width="780" height="439" src="https://www.youtube.com/embed/s55BEAJRTOk?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 5: Use Slicing</h2>
<p class="has-global-color-8-background-color has-background">This method uses Python’s infamous <a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-slicing-in-python/" data-type="URL" data-id="https://blog.finxter.com/introduction-to-slicing-in-python/" target="_blank"><code>Slicing</code></a>! No overhead, and a very pythonic way to resolve the issue.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">prime_nums = [2, 3, 5, 7, 11]
prime_nums[:] = [x * 2 for x in prime_nums]
print(prime_nums)</pre>
<p>Above declares the first (5) <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Prime_number" target="_blank">Prime Numbers</a> and saves them to <code>prime_nums</code>.</p>
<p>Then slicing is applied and used in conjunction with <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank"><code>List Comprehension</code></a> to apply the multiplication operation to each element. The results save back to <code>prime_nums</code> and are output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>[4, 6, 10, 14, 22]</code></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="The Ultimate Guide to Slicing in Python" width="780" height="439" src="https://www.youtube.com/embed/D2ZueuWXST8?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
<p class="has-text-align-center has-base-3-color has-accent-background-color has-text-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f31f.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />A Finxter Favorite!</p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Summary</h2>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">These methods of multiplying list elements by a number should give you enough information to select the best one for your coding requirements.</p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Good Luck & Happy Coding!</p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Programmer Humor</h2>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f471-200d-2640-fe0f.png" alt="?♀️" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Programmer 1</strong>: We have a problem<br /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f9d4-200d-2642-fe0f.png" alt="?♂️" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Programmer 2</strong>: Let’s use RegEx!<br /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f471-200d-2640-fe0f.png" alt="?♀️" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Programmer 1</strong>: Now we have two problems</p>
<p>… yet – you can easily reduce the two problems to zero as you polish your “<a rel="noreferrer noopener" href="https://blog.finxter.com/python-regex/" data-type="post" data-id="6210" target="_blank">RegEx Superpower in Python</a>“. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
</p></p>
</div>
https://www.sickgaming.net/blog/2022/07/...op-5-ways/