{"id":128014,"date":"2022-09-11T13:02:15","date_gmt":"2022-09-11T13:02:15","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=661539"},"modified":"2022-09-11T13:02:15","modified_gmt":"2022-09-11T13:02:15","slug":"how-to-apply-a-function-to-a-list","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/09\/11\/how-to-apply-a-function-to-a-list\/","title":{"rendered":"How to Apply a Function to a List"},"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;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;}\">\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; (2 votes) <\/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\">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>\n<p>To make it more interesting, we have the following running scenario:<\/p>\n<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>\n<hr class=\"wp-block-separator has-alpha-channel-opacity wp-embed-aspect-16-9 wp-has-aspect-ratio\"\/>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background\"><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 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>\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 <\/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>\n<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>\n<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>\n<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>\n<li><strong>Bonus<\/strong>: Calculate Commissions on each List Element<\/li>\n<\/ul>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Preparation<\/h2>\n<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>\n<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>\n<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>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Method 1: Use a Generator Expression<\/h2>\n<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>\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=\"\">nums = [18, 43, 54, 65, 31, 21, 27]\nnums = (math.pow(num,2) for num in nums)\nprint(nums)<\/pre>\n<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>\n<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&#8217;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>\n<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>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>&lt;generator object at 0x000002468D9B59A0><\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<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>\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=\"\">print(list(nums))<\/pre>\n<p>The content of <code>nums <\/code>is as follows.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td>[324.0, 1849.0, 2916.0, 4225.0, 961.0, 441.0, 729.0]<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<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 decoding=\"async\" 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>\n<p class=\"has-global-color-8-background-color has-background\"><strong><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" 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>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Method 2: Use List Comprehension<\/h2>\n<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>\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=\"\">nums = [18, 43, 54, 65, 31, 21, 27]\nnums = [math.sqrt(num) for num in nums]\nprint(nums)<\/pre>\n<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>\n<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&#8217;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>\n<p>If output to the terminal, the following displays.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>[4.242640687119285, 6.557438524302, 7.3484692283495345, 8.06225774829855, 5.5677643628300215, 4.58257569495584, 5.196152422706632]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<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 decoding=\"async\" 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>\n<p class=\"has-global-color-8-background-color has-background\"><strong><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" 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>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Method 3: Use a Lambda and map()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example uses Python&#8217;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>\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=\"\">nums = [18, 43, 54, 65, 31, 21, 27]\nnums = list(map(lambda x: math.degrees(x), nums))\nprint(nums)<\/pre>\n<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>\n<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&#8217;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>\n<p>If output to the terminal, the following displays.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code> [1031.324031235482, 2463.71851906254, 3093.9720937064453, 3724.225668350351, 1776.169164905552, 1203.2113697747288, 1546.9860468532227]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<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 decoding=\"async\" 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>\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\/1f4a1.png\" alt=\"\ud83d\udca1\" 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>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Method 4: Use a For Loop<\/h2>\n<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>\n<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]\ni = 0 while i &lt; len(nums): nums[i] = round(math.sqrt(nums[i]), 2) i += 1 print(nums)<\/pre>\n<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>\n<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>\n<p>Upon completion of the iteration, the output is sent to the terminal.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code> [4.24, 6.56, 7.35, 8.06, 5.57, 4.58, 5.2]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Bonus: Calculate Commissions on each List Element<\/h2>\n<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>\n<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>\n<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'])\nstreet = [item.title() for item in street] prices = list(df['price'])\ncommis = [round(p*.06,2) for p in prices] print(street)\nprint(prices)<\/pre>\n<p>The output it as follows.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<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>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p class=\"has-text-align-center has-contrast-color has-global-color-8-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;\" \/>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>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Summary<\/h2>\n<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>\n<p>Good Luck &amp; Happy Coding!<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Programming Humor &#8211; Python<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" 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\/uploads\/2022\/07\/image-65-264x300.png 264w\" sizes=\"auto, (max-width: 518px) 100vw, 518px\" \/><figcaption><em>&#8220;I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I&#8217;m leaving you.&#8221;<\/em> &#8212; <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>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (2 votes) Problem Formulation and Solution Overview This article will show you how to apply a function to a List in Python. To make it more interesting, we have the following running scenario: As a Python assignment, you have been given a List of Integers and asked to apply a function to each [&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-128014","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\/128014","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=128014"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128014\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=128014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=128014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=128014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}