This article shows how to do math with lists.
Contents
- The four arithmetic operations with elements of a List
- Average of elements of a List
- How to calculate the elements between two lists using "for loop"
- How to calculate the elements between two lists using zip( ) function
- How to calculate the elements between two lists using map( ) function and lambda
sponsored link
The four arithmetic operations with elements of a List
In the beginning, I show the basics of the four arithmetic operations with elements of a List.
These are the arithmetic operators in Python.
Arithmetic Operators
Addition : x + y
Subtraction : x - y
Multiplication : x * y
Division : x / y
Modulus : x % y
Power : x ** y
Modulus: returns the remainder when first operand is divided by the second
Power : Returns first raised to power second
Addition of elements in a list [ using "for loop" ]
I show how to calculate addition of elements in a list using "for loop".
Here is an example code.
num_list = [1, 2, 3, 4, 5]
tmp = 0
sum_num = 0
for i in range(len(num_list)):
tmp = num_list[i] + tmp
print(tmp)
# 15
The elements in the list are added by the following repetition.
i = 0 : tmp = num_list[0] + tmp = 1 + 0
i = 1 : tmp = num_list[1] + tmp = 2 + 1
Addition of elements in a list [ using "sum() function" ]
Addition of elements in a list can be easily calculated by using the sum( ) function.
Here is an example code.
num_list = [1, 2, 3, 4, 5]
print(sum(num_list))
# 15
sponsored link
Average of elements of a List
I show how to calculate average of elements in a list.
Two methods are described in this article.
- Using "sum() and len() functions"
- Using "statistics" of library
Average of elements in a list [ using "sum() and len() functions" ]
I show how to calculate average of elements in a list by using "sum() and len() functions".
Here is an example code.
num_list = [1, 2, 3, 4, 5]
mean = sum(num_list)/len(num_list)
print(mean)
# 3.0
Average of elements in a list [ using "statistics" of library]
Average of elements in a list can be easily calculated by using "statistics" of library.
How to write
import statistics
statistics.mean( List )
Here is an example code.
import statistics
num_list = [1, 2, 3, 4, 5]
mean = statistics.mean(num_list)
print(mean)
# 3
sponsored link
How to calculate the elements between two lists using "for loop"
In the following sections, I will show you how to calculate the elements between the two lists.
First, I will show you how to calculate using "for loop".
Here is an example code.
num_list_1 = [1, 2, 3, 4, 5]
num_list_2 = [10, 20, 30, 40, 50]
tmp = 0
num_list_3 = []
for i in range(len(num_list_1)):
tmp = num_list_1[i] + num_list_2[i]
num_list_3.append(tmp)
print(num_list_3)
# [11, 22, 33, 44, 55]
sponsored link
How to calculate the elements between two lists using zip( ) function
I show how to calculate the elements between two lists using zip( ) function.
The elements of multiple lists can be retrieved at once by using zip( ) function.
Here is an sample code.
num_list_1 = [1, 2, 3, 4, 5]
num_list_2 = [10, 20, 30, 40, 50]
num_list_3 = [x + y for x, y in zip(num_list_1, num_list_2)]
print(num_list_3)
# [11, 22, 33, 44, 55]
sponsored link
How to calculate the elements between two lists using map( ) function and lambda
I show how to calculate the elements between two lists using map( ) function and lambda.
The map() function can be used to apply the processing of a particular function to all elements of a list.
Lambda is used to define functions.
Here is an example code.
num_list_1 = [1, 2, 3, 4, 5]
num_list_2 = [10, 20, 30, 40, 50]
num_list_3 = [x + y for x, y in zip(num_list_1, num_list_2)]
print(num_list_3)
# [11, 22, 33, 44, 55]
By the way, the following article describes how to use map( ) function and lambda.
How to use map() Function.
How to use Lambda Function.
sponsored link