{"id":4220,"date":"2020-03-23T10:51:36","date_gmt":"2020-03-23T10:51:36","guid":{"rendered":"https:\/\/www.askpython.com\/?p=4220"},"modified":"2020-03-23T10:51:37","modified_gmt":"2020-03-23T10:51:37","slug":"average-of-a-list-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/list\/average-of-a-list-in-python","title":{"rendered":"Find the Average of a List in Python with 5 Easy Methods"},"content":{"rendered":"\n<p>There are many ways to find the average of a list in Python, provided all of them are of the same type. In this article, we&#8217;ll look at some of the methods to find the average element of a <a href=\"https:\/\/www.askpython.com\/python\/list\/python-list\" class=\"rank-math-link\">Python List<\/a>.<\/p>\n\n\n\n<p>Let&#8217;s get started!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Method 1: Use reduce() to find the average of a list in Python<\/h2>\n\n\n\n<p>We can use the <strong>reduce<\/strong>() method, along with a lambda function (As shown <a class=\"rank-math-link rank-math-link\" href=\"https:\/\/www.askpython.com\/python\/python-lambda-anonymous-function#lambda-function-with-reduce\">here<\/a>).<\/p>\n\n\n\n<p>We&#8217;ll sum up the elements using the lambda, and divide the result by the length of the list since <strong>average = (Sum of all elements) \/ (Number of elements)<\/strong><\/p>\n\n\n\n<p>So, the corresponding expression is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\naverage = (reduce(lambda x, y: x + y, input_list)) \/ len(input_list)\n<\/pre><\/div>\n\n\n<p>Let&#8217;s take a full example, to show this in action.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom functools import reduce\n\ninput_list = &#x5B;3, 2, 1, 5, 7, 8]\n\naverage = (reduce(lambda x, y: x + y, input_list)) \/ len(input_list)\n\nprint(&quot;Average of List:&quot;, average)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nAverage of List: 4.333333333333333\n<\/pre><\/div>\n\n\n<p>This is indeed the correct output, since the average is:<strong> (3 + 2 + 1 + 5 + 7 + 8) \/ 6 = 26 \/ 6 = 4.333<\/strong><\/p>\n\n\n\n<p>We cannot pass elements of different types, since the <code>+<\/code> operand may not work. So, all the elements of the list must be type compatible with the <code>+<\/code> operand in order for this to work. This is one of the methods we can find the average of a list in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method 2: Using the sum() method (Recommended)<\/h2>\n\n\n\n<p>Instead of using the <code>reduce()<\/code> method, we can directly find the sum using <code>sum(list)<\/code>, and divide it by the length.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninput_list = &#x5B;3, 2, 1, 5, 7, 8]\n\naverage = sum(input_list) \/ len(input_list)\n\nprint(&quot;Average of List:&quot;, average)\n<\/pre><\/div>\n\n\n<p>We&#8217;ll get the same output as before!<\/p>\n\n\n\n<p>This is the recommended method of finding the average of a list in Python, since it uses only built-in functions, and is really simple to use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method 3: Using statistics.mean()<\/h2>\n\n\n\n<p>For Python versions of <strong>3.4<\/strong> and above, we can also use the <code>statistics<\/code> module to find the average of a list in Python.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport statistics\n\ninput_list = &#x5B;3, 2, 1, 5, 7, 8]\n\naverage = statistics.mean(input_list)\n\nprint(&quot;Average of List:&quot;, average)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Method 4: Using numpy.mean()<\/h2>\n\n\n\n<p>If you want to use <strong>NumPy<\/strong> based methods, <code>numpy.mean()<\/code> will do this for you.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport numpy as np\n\ninput_list = &#x5B;3, 2, 1, 5, 7, 8]\n\naverage = np.mean(input_list)\n\nprint(&quot;Average of List:&quot;, average)\n<\/pre><\/div>\n\n\n<p>You can also convert the list into a <strong>numpy array<\/strong>, and then get the mean using <code>ndarray.mean<\/code>. You can use whichever is better for your use-case, but they perform about the same.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport numpy as np\n\ninput_list = &#x5B;3, 2, 1, 5, 7, 8]\n\n# Convert to a numpy ndarray and get the mean\n# using ndarray.mean\naverage = np.array(input_list).mean\n\nprint(&quot;Average of List:&quot;, average)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Method 5: Using pandas Series.mean()<\/h2>\n\n\n\n<p>If you&#8217;re using <code>Pandas<\/code>, you can get the mean of a list using <code>pandas.Series(list).mean()<\/code><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pandas as pd\n\ninput_list = &#x5B;3, 2, 1, 5, 7, 8]\n\n# Convert the list to a Pandas Series using pd.Series(list)\n# and then get it&#039;s mean using series.mean()\naverage = pd.Series(input_list).mean()\n\nprint(&quot;Average of List:&quot;, average)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nAverage of List: 4.333333333333333\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we learned how we could find the average of a list in Python, using different approaches.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a class=\"rank-math-link rank-math-link\" href=\"https:\/\/stackoverflow.com\/questions\/9039961\/finding-the-average-of-a-list\" target=\"_blank\" rel=\"noopener\">StackOverflow Question<\/a> on Finding the Average of a List<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n","protected":false},"excerpt":{"rendered":"<p>There are many ways to find the average of a list in Python, provided all of them are of the same type. In this article, we&#8217;ll look at some of the methods to find the average element of a Python List. Let&#8217;s get started! Method 1: Use reduce() to find the average of a list [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":4232,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-4220","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-list"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=4220"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4220\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/4232"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=4220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=4220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=4220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}