Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Apply a Function to a List

#1
How to Apply a Function to a List

<div>
<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;661539&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;2&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 - (2 votes)&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;}">
<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 – (2 votes) </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">This article will show you how to apply a function 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">List</a> in Python.</p>
<p>To make it more interesting, we have the following running scenario:</p>
<p><em>As a Python assignment, you have been given 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"></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">List</a> of Integers and asked to apply a function to each <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> element in various ways.</em></p>
<hr class="wp-block-separator has-alpha-channel-opacity wp-embed-aspect-16-9 wp-has-aspect-ratio"/>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background"><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 code to apply a function 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"></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">List</a> in Python<em>?</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 </strong>1: Use a <a rel="noreferrer noopener" href="https://blog.finxter.com/understanding-generators-in-python/" data-type="URL" data-id="https://blog.finxter.com/understanding-generators-in-python/" target="_blank">Generator Expression</a></li>
<li><strong>Method </strong>2: 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">List Comprehension</a></li>
<li><strong>Method </strong>3: Use 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>, <a rel="noreferrer noopener" href="https://blog.finxter.com/python-map/" data-type="URL" data-id="https://blog.finxter.com/python-map/" target="_blank"><code>map()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"></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">List</a> </li>
<li><strong>Method 4</strong>: Use 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"><code>for</code></a> Loop</li>
<li><strong>Bonus</strong>: Calculate Commissions on each List Element</li>
</ul>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Preparation</h2>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">These examples use functions from the <a rel="noreferrer noopener" href="https://docs.python.org/3/library/math.html" data-type="URL" data-id="https://docs.python.org/3/library/math.html" target="_blank"><code>math</code></a> library.</p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Add the following code to the top of each script. This snippet will allow the code in this article to run error-free.</p>
<pre class="EnlighterJSRAW wp-embed-aspect-16-9 wp-has-aspect-ratio" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import math</pre>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Method 1: Use a Generator Expression</h2>
<p class="has-global-color-8-background-color has-background">This example uses a <a rel="noreferrer noopener" href="https://blog.finxter.com/understanding-generators-in-python/" data-type="URL" data-id="https://blog.finxter.com/understanding-generators-in-python/" target="_blank">Generator Expression</a>. This expression performs any operations in memory first and returns an iterable object. An efficient option as upon completion, memory is cleared, and variables erased.</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="">nums = [18, 43, 54, 65, 31, 21, 27]
nums = (math.pow(num,2) for num in nums)
print(nums)</pre>
<p>The above code declares 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">List</a> of Integers and saves it to the variable <code>nums</code>.</p>
<p>Next, a <a rel="noreferrer noopener" href="https://blog.finxter.com/understanding-generators-in-python/" data-type="URL" data-id="https://blog.finxter.com/understanding-generators-in-python/" target="_blank">Generator Expression</a> is called and applies the <a rel="noreferrer noopener" href="https://blog.finxter.com/a-guide-to-pythons-pow-function/" data-type="URL" data-id="https://blog.finxter.com/a-guide-to-pythons-pow-function/" target="_blank"><code>math.pow()</code></a> function from Python’s built-in <a rel="noreferrer noopener" href="https://docs.python.org/3/library/math.html" data-type="URL" data-id="https://docs.python.org/3/library/math.html" target="_blank"><code>math</code></a> library to each list element. The results save back to <code>nums</code>. </p>
<p>If output to the terminal at this point, an <a href="https://blog.finxter.com/iterators-iterables-and-itertools/" data-type="post" data-id="29507" target="_blank" rel="noreferrer noopener">iterable</a> Generator Object similar to the following displays.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>&lt;generator object at 0x000002468D9B59A0></code></td>
</tr>
</tbody>
</table>
</figure>
<p>To turn the Generator Object into a <a href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/"></a><code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a></code>, run the following code.</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="">print(list(nums))</pre>
<p>The content of <code>nums </code>is as follows.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>[324.0, 1849.0, 2916.0, 4225.0, 961.0, 441.0, 729.0]</td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-apply-a-function-to-a-list/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F2Ls0b1gjz2A%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<p class="has-global-color-8-background-color has-background"><strong><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />Note</strong>: The <a rel="noreferrer noopener" href="https://blog.finxter.com/a-guide-to-pythons-pow-function/" data-type="URL" data-id="https://blog.finxter.com/a-guide-to-pythons-pow-function/" target="_blank"><code>math.pow()</code></a> function accepts two (2) integers as arguments: <code>x</code> (the value) and <code>y</code> (the power), and returns the value of <code>x</code> raised to the power of <code>y</code>.</p>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Method 2: Use List Comprehension</h2>
<p class="has-global-color-8-background-color has-background">This example uses <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" target="_blank">List Comprehension</a> to perform an operation on each <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> element.</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="">nums = [18, 43, 54, 65, 31, 21, 27]
nums = [math.sqrt(num) for num in nums]
print(nums)</pre>
<p>The above code declares 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">List</a> of Integers and saves it to the variable <code>nums</code>.</p>
<p>Next, <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" target="_blank">List Comprehension</a> is called and applies the<a rel="noreferrer noopener" href="https://blog.finxter.com/a-guide-to-pythons-pow-function/" data-type="URL" data-id="https://blog.finxter.com/a-guide-to-pythons-pow-function/" target="_blank"> </a><a rel="noreferrer noopener" href="https://docs.python.org/3/library/math.html" data-type="URL" data-id="https://docs.python.org/3/library/math.html" target="_blank"><code>math.sqrt()</code></a> function from Python’s built-in <a rel="noreferrer noopener" href="https://docs.python.org/3/library/math.html" data-type="URL" data-id="https://docs.python.org/3/library/math.html" target="_blank"><code>math</code></a> library to each <a href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/"></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">List</a> element. The results save back to <code>nums</code>. </p>
<p>If output to the terminal, the following displays.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>[4.242640687119285, 6.557438524302, 7.3484692283495345, 8.06225774829855, 5.5677643628300215, 4.58257569495584, 5.196152422706632]</code></td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-apply-a-function-to-a-list/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FaBC0VhpXkOQ%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<p class="has-global-color-8-background-color has-background"><strong><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />Note</strong>: The <a rel="noreferrer noopener" href="https://docs.python.org/3/library/math.html" data-type="URL" data-id="https://docs.python.org/3/library/math.html" target="_blank"><code>math.sqrt()</code></a> function accepts an integer as an argument and returns the square root of said argument.</p>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Method 3: Use a Lambda and map()</h2>
<p class="has-global-color-8-background-color has-background">This example uses Python’s <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 combined with <a rel="noreferrer noopener" href="https://blog.finxter.com/python-map/" target="_blank"><code>map()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank"></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">List</a> to apply a mathematical operation to each <a href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank" rel="noreferrer noopener">List</a> element.</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="">nums = [18, 43, 54, 65, 31, 21, 27]
nums = list(map(lambda x: math.degrees(x), nums))
print(nums)</pre>
<p>The above code declares 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"></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">List</a> of numbers and saves it to the variable <code>nums</code>.</p>
<p>Next, <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> is called and passed an argument <a rel="noreferrer noopener" href="https://blog.finxter.com/python-map/" target="_blank"><code>map()</code></a>, which in turn passes 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 to apply the <a rel="noreferrer noopener" href="https://docs.python.org/3/library/math.html" data-type="URL" data-id="https://docs.python.org/3/library/math.html" target="_blank"><code>math.degrees()</code></a> function from Python’s built-in <a rel="noreferrer noopener" href="https://docs.python.org/3/library/math.html" data-type="URL" data-id="https://docs.python.org/3/library/math.html" target="_blank"><code>math</code></a> library to each <a href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/"></a><code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">L</a></code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">i</a><code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">st</a></code> element. The result returns to <code>nums</code>.</p>
<p>If output to the terminal, the following displays.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code> [1031.324031235482, 2463.71851906254, 3093.9720937064453, 3724.225668350351, 1776.169164905552, 1203.2113697747288, 1546.9860468532227]</code></td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-apply-a-function-to-a-list/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FkBg4n52XoUQ%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><code>Note</code>: The <a rel="noreferrer noopener" href="https://docs.python.org/3/library/math.html" data-type="URL" data-id="https://docs.python.org/3/library/math.html" target="_blank"><code>math.degrees()</code></a> function accepts an angle as an argument, converts this argument from radians to degrees and returns the result.</p>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Method 4: Use a For Loop</h2>
<p class="has-global-color-8-background-color has-background">This example uses 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"><code>for</code></a> Loop to apply a mathematical operation to each List element.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">nums = [18, 43, 54, 65, 31, 21, 27]
i = 0 while i &lt; len(nums): nums[i] = round(math.sqrt(nums[i]), 2) i += 1 print(nums)</pre>
<p>The above code declares 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"></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">List</a> of Integers and saves it to the variable <code>nums</code>. Then, a counter variable, <code>i</code> is declared, set to 0.</p>
<p>Next, 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"><code>while</code></a> loop is instantiated and iterates through each <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> element, applying the <a rel="noreferrer noopener" href="https://blog.finxter.com/a-guide-to-pythons-pow-function/" data-type="URL" data-id="https://blog.finxter.com/a-guide-to-pythons-pow-function/" target="_blank"> </a><a rel="noreferrer noopener" href="https://docs.python.org/3/library/math.html" data-type="URL" data-id="https://docs.python.org/3/library/math.html" target="_blank"><code>math.sqrt()</code></a> function, and limiting the decimal places to two (2). The results save back to the appropriate element in <code>nums</code>.</p>
<p>Upon completion of the iteration, the output is sent to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code> [4.24, 6.56, 7.35, 8.06, 5.57, 4.58, 5.2]</code></td>
</tr>
</tbody>
</table>
</figure>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Bonus: Calculate Commissions on each List Element</h2>
<p class="has-global-color-8-background-color has-background">This bonus code extracts two (2) columns from a <a rel="noreferrer noopener" href="https://blog.finxter.com/wp-content/uploads/2022/02/real-estate.csv" data-type="URL" data-id="https://blog.finxter.com/wp-content/uploads/2022/02/real-estate.csv" target="_blank"><code>real-estate.csv</code></a> file, the street and price columns and converts each into 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">List</a>.</p>
<p>Then, the street column is converted from <strong>UPPERCASE</strong> <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-upper/" data-type="URL" data-id="https://blog.finxter.com/python-string-upper/" target="_blank"><code>uppercase()</code></a> to <strong>Title Case</strong> by applying the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-title/" data-type="URL" data-id="https://blog.finxter.com/python-string-title/" target="_blank"></a><a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-title/" data-type="URL" data-id="https://blog.finxter.com/python-string-title/" target="_blank"><code>title()</code></a> function. Next, Sales Commissions are calculated and applied to each price element using <a rel="noreferrer noopener" href="https://blog.finxter.com/python-round-a-simple-guide-with-video/" data-type="URL" data-id="https://blog.finxter.com/python-round-a-simple-guide-with-video/" target="_blank"><code>round()</code></a>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd df = pd.read_csv('real-estate.csv', usecols=['street', 'price']).head(5) street = list(df['street'])
street = [item.title() for item in street] prices = list(df['price'])
commis = [round(p*.06,2) for p in prices] print(street)
print(prices)</pre>
<p>The output it as follows.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code> ['3526 High St', '51 Omaha Ct', '2796 Branch St', '2805 Janette Way', '6001 Mcmahon Dr']<br />[59222, 68212, 68880, 69307, 81900]</code></td>
</tr>
</tbody>
</table>
</figure>
<p class="has-text-align-center has-contrast-color has-global-color-8-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;" />Finxter Challenge!<br />Convert these Lists into a <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-convert-two-lists-into-a-dictionary/" data-type="URL" data-id="https://blog.finxter.com/how-to-convert-two-lists-into-a-dictionary/" target="_blank">Dictionary</a> format.</p>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Summary</h2>
<p>This article has provided four (4) ways to apply a function to each <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> element to select the best fit for your coding requirements.</p>
<p>Good Luck &amp; Happy Coding!</p>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Programming Humor – Python</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="518" height="588" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-65.png" alt="" class="wp-image-471102" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-65.png 518w, https://blog.finxter.com/wp-content/uplo...64x300.png 264w" sizes="(max-width: 518px) 100vw, 518px" /><figcaption><em>“I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I’m leaving you.”</em> — <a rel="noreferrer noopener" href="https://imgs.xkcd.com/comics/python.png" data-type="URL" data-id="https://imgs.xkcd.com/comics/python.png" target="_blank">xkcd</a></figcaption></figure>
</div>
</div>


https://www.sickgaming.net/blog/2022/09/...to-a-list/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016