Sum list of list Python

In this post, you’ll learn how to use Python to find the average of a list, as well as of a list of list. You’ll learn how to do this using built-in methods like for-loops, the numpy library, and the statistics library.

The Python Average is calculated by adding up the items in a list and dividing it by the number of items in that list (which can be found using the length of that list).

Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!

How can you calculate an average in Python?

Calculating the average of different values in Python is very straightforward. You simply add up an array of numbers and divide it by the length of that array.

One naive way that you can calculate the average in Python is using a for loop.

Let’s see how this can be done:

# Calculating the average of list using a for loop numbers = [1,2,3,4,5,6,7,8,9] sum = 0 count = 0 for number in numbers: sum += number count += 1 average = sum / count print(average) # Returns: 5.0

This is a fairly labour-intensive way of calculating an average in Python, but it shows how it can be done.

Calculate Average of Python List using sum and len

Python doesn’t have a built-in function to calculate an average of a list, but you can use the sum() and len() functions to calculate an average of a list.

In order to do this, you first calculate the sum of a list and then divide it by the length of that list. Let’s see how we can accomplish this:

# Calculating the average of a list using sum() and len() numbers = [1,2,3,4,5,6,7,8,9] average = sum(numbers) / len(numbers) # Returns 5.0

If you find yourself doing this frequently in a script, you can turn this into a function. That way you can pass in any list and return its average. let’s see how to do this:

# Creating a function to calculate an average of a list numbers = [1,2,3,4,5,6,7,8,9] def average_of_list(list_to_use): return sum(list_to_use) / len(list_to_use) print(average_of_list(numbers)) # Returns: 5.0

In the example above, you turned the simple calculation of dividing the sum of a list by its length into a function to make it re-usable throughout your scripts.

Calculate Average of Python List using Numpy

If you’re working with numbers, chances are you’re using numpy in your script, meaning you can easily use some of its built-in functions. Numpy comes built-in with a number of mathematical functions that make it easy to calculate various statistics, such as an average.

Let’s see how we can work with numpy to calculate the average of a list:

# Using numpy to calculate the average of a list from numpy import mean numbers = [1,2,3,4,5,6,7,8,9] average = mean(numbers) print(average) # Returns: 5.0

Let’s see what we’ve done here:

  1. We imported mean from numpy, which is a function used to calculate the mean
  2. We then load our list numbers
  3. We then create a new variable and use the mean function, passing in the list as the argument

Next, let’s see how we can calculate the average of a Python list using the statistics library.

Find Average of List Using the Statistics Library

The statistics library is a Python library that lets you calculate, well, statistics. Similar to numpy, the statistics library has a mean function built into it. Let’s see how we can use this function to calculate the mean of a Python list:

# Using the statistics library to calculate a mean of a list from statistics import mean numbers = [1,2,3,4,5,6,7,8,9] average = mean(numbers) print(average) # Returns: 5.0

Let’s see what we accomplished here:

  1. We imported the mean() function from the statistics library
  2. We loaded in our list of numbers
  3. We passed this list in as an argument to the mean() function

Find Average of Python List of Lists

There may be many times when you want to calculate the average of a list of lists.

But first: what is a list of lists? A list of list is, simply, a list that contains other lists.

Let’s say you’re given a list of list like below:

list_of_lists = [ [1,2,3], [4,5,6], [7,8,9] ]

And you wanted to turn it into this:

row_averages = [2.0, 5.0, 8.0] column_averages = [4.0, 5.0, 6.0]

Use Zip to Calculate the Average of a List of Lists

We can use the Python zip() function to calculate the average of a list of lists. To learn more about the zip() function, check you my tutorial here. We can use the zip() function in conjunction with list comprehensions, which you can learn about in my video here:

Let’s see how we can accomplish this:

# Calculating an average of a list of lists using zip() list_of_lists = [ [1,2,3], [4,5,6], [7,8,9] ] column_average = [sum(sub_list) / len(sub_list) for sub_list in zip(*list_of_lists)] row_average = [sum(sub_list) / len(sub_list) for sub_list in list_of_lists] print(column_average) print(row_average) # Returns: # [4.0, 5.0, 6.0] # [2.0, 5.0, 8.0]

Let’s see what we’ve done here:

  1. We loaded a list of lists and assigned it to list_of_lists
  2. We then calculated by a row-wise average and a column-wise average
  3. The column-wise average uses the zip() function to access to ith item in each list
  4. The row-wise average calculates the average of each sublist

Use Numpy to Calculate the Average of a List of Lists

Similar to above, we can use numpy to calculate the average of a list of lists. In order to calculate a row-wise and a column-wise mean of a list of lists, we can simply modify the axis= parameter.

The numpy implementation is a bit easier to read, though it does involve importing an additional package, if you’re not already using it:

list_of_lists = [ [1,2,3], [4,5,6], [7,8,9] ] from numpy import array, average array_lol = array(list_of_lists) column_average = average(array_lol, axis=0) row_average = average(array_lol, axis=1) print(column_average) print(row_average) # Returns # [2. 5. 8.] # [4. 5. 6.]

We can see that this is a bit easier to write and to read. We use the axis=0 to calculate across columns, and axis=1 to calculate across rows.

Conclusion

In this post, you learned how to calculate the Python average of a list and of lists of lists. You learned how to do this with built-in functions including sum and len, as well as external packages including numpy and statistics. Finally, you learned how to calculate both row-wise and column-wise averages of lists of lists.

To learn more about the numpy average function, check out the official documentation here.

In this short tutorial, we look at how we can use Python to find the sum() of a list. We look at the various methods to do this along with their limitations.

Table of Contents - Python Sum List:

  1. Using Sum to find the sum of a List
  2. How to use the sum() function?
  3. Limitation and Caverts - Python Sum List
  4. Other Related Concepts

Python Sum List:

While using Python, there are sure to be numerous use cases where you might have to calculate the sum of an iterable. For the purpose of this blog, we mainly focus on lists; however, the same method can be applied to other iterables as well.

An example of a use case is the use of sum() to return the sum of a list that contains the weekly income of employees to calculate monthly income.

How to use the sum() function?

The sum() function returns the sum of an iterable. Sum() takes a list (iterable) and returns the sum of the numbers within the list.

The syntax is as follows:

sum(iterable, start)

Parameters:

  1. Iterable - Required, iterable can be a list, tuples, and dictionary
  2. Start - Optional, if passed it the value will be added returned sum


Code and Explanation:

#Using range to create list of numbers numbers = list(range(0,10)) sum_numbers = sum(numbers) print(sum_numbers) #Output - 45 #Passing an argument as start sum_numbers = sum(numbers, 10) print(sum_numbers) #Output - 55 As seen in the above code snippet, the sum() function is used to add the values in the range that has been specified. You can similarly use the function for various operations.

Limitations and Caveats - Python Sum List

A common error that arises while using the sum() function, is when the list contains a string. Since it is not possible to add int values in strings, Python returns a TypeError. Let us look at such an instance.#Creating a list of number and a string numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "10"] sum_numbers = sum(numbers) print(sum_numbers) Python returns this output:
Traceback (most recent call last): File "C:\Users\Python\Using_sum.py", line 4, in sum_numbers = sum(numbers) TypeError: unsupported operand type(s) for +: 'int' and 'str'
As explained, the int value in the string causes the TypeError. Other than this limitation, you can make use of the sum() function in Python with ease for all summing operations.

The adjacent elements in the array are compared and the positions are swapped if the first element is greater than the second. In this fashion, the largest value "bubbles" to the top.

You might find this useful when you want to execute a particular script if the array is empty - like enabling or disabling buttons based on if there is any input in the required field, etc. 

In this article, we cover the different methods to compare two arrays and check if they are equal using javascript. In this process we will also understand the pros and cons of various methods used. 

Video liên quan

Chủ đề