{"id":126474,"date":"2022-07-12T15:36:28","date_gmt":"2022-07-12T15:36:28","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=467518"},"modified":"2022-07-12T15:36:28","modified_gmt":"2022-07-12T15:36:28","slug":"how-to-multiply-list-elements-by-a-number-top-5-ways","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/07\/12\/how-to-multiply-list-elements-by-a-number-top-5-ways\/","title":{"rendered":"How to Multiply List Elements by a Number \u2013 Top 5 Ways"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;467518&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<h2 class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">Problem Formulation and Solution Overview<\/h2>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">In this article, you&#8217;ll learn <strong><em>how to multiply List Elements by a Number in Python<\/em><\/strong>.<\/p>\n<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>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background\"><em><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" 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>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">We can accomplish this task by one of the following options:<\/p>\n<ul type=\"video\" class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<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>\n<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>\n<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>\n<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>\n<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>\n<\/ul>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Method 1: Use List Comprehension <\/h2>\n<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>\n<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]\nmult_result = [x * 2 for x in prime_nums]\nprint(mult_result)<\/pre>\n<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>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>[4, 6, 10, 14, 22]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<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>\n<\/div>\n<\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Method 2: Use Pandas tolist()<\/h2>\n<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>\n<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]\nmult_result = pd.Series(prime_nums)\nmult_result = (mult_result*2).tolist()\nprint(mult_result)<\/pre>\n<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>\n<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>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>0 2<br \/>1 3<br \/>2 5<br \/>3 7<br \/>4 11<br \/>dtype: int64<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<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>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>[4, 6, 10, 14, 22]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Method 3: Use map and lambda Functions<\/h2>\n<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>\n<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]\nmult_result = list(map(lambda x: x*2, prime_nums))\nprint(mult_result)<\/pre>\n<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>\n<ul>\n<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>\n<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>&lt;map object at 0x000001DC99CBBBB0&gt;<\/code><\/li>\n<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>\n<li>The results save to <code>mult_result<\/code>.<\/li>\n<\/ul>\n<p>Then, <code>mult_result<\/code> is output to the terminal. <\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>[4, 6, 10, 14, 22]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Let&#039;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>\n<\/div>\n<\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Method 4: Use Numpy Array()<\/h2>\n<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>\n<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]\nthe_result = list(np.array(prime_nums) * 2)\nprint(the_result)<\/pre>\n<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>\n<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>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>[4, 6, 10, 14, 22]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<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>\n<\/div>\n<\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Method 5: Use Slicing<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This method uses Python&#8217;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>\n<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]\nprime_nums[:] = [x * 2 for x in prime_nums]\nprint(prime_nums)<\/pre>\n<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>\n<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>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>[4, 6, 10, 14, 22]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<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>\n<\/div>\n<\/figure>\n<p class=\"has-text-align-center has-base-3-color has-accent-background-color has-text-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f31f.png\" alt=\"\ud83c\udf1f\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>A Finxter Favorite!<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2 class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">Summary<\/h2>\n<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>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">Good Luck &amp; Happy Coding!<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Programmer Humor<\/h2>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f471-200d-2640-fe0f.png\" alt=\"\ud83d\udc71\u200d\u2640\ufe0f\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Programmer 1<\/strong>: We have a problem<br \/><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f9d4-200d-2642-fe0f.png\" alt=\"\ud83e\uddd4\u200d\u2642\ufe0f\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Programmer 2<\/strong>: Let\u2019s use RegEx!<br \/><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f471-200d-2640-fe0f.png\" alt=\"\ud83d\udc71\u200d\u2640\ufe0f\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Programmer 1<\/strong>: Now we have two problems<\/p>\n<p>&#8230; yet &#8211; you can easily reduce the two problems to zero as you polish your &#8220;<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-regex\/\" data-type=\"post\" data-id=\"6210\" target=\"_blank\">RegEx Superpower in Python<\/a>&#8220;. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f642.png\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Problem Formulation and Solution Overview In this article, you&#8217;ll learn how to multiply List Elements by a Number in Python. This example multiples the first five (5) Prime Numbers by two (2) and return the result. Question: How would we write Python code to multiply the list elements? We can accomplish [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-126474","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126474","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=126474"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126474\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=126474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=126474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=126474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}