Subtract a Value from every Number in a List in Python

Borislav Hadzhiev

Last updated: Apr 10, 2024

Reading timeยท4 min

- Subtract a value from every number in a List in Python
- Subtract a value from every number in a List using a
`for`

loop - Subtract a value from every number in a List using NumPy
- Subtract a value from every number in a List using
`map()`

- Subtract a value from every number in a List using enumerate

**To subtract a value from every number in a list:**

- Use a list comprehension to iterate over the list.
- Use the subtraction
`-`

operator to subtract a value from every number. - The new list will contain the results.

main.py

`a_list = [5, 15, 25, 35, 45] a_list = [item - 5 for item in a_list] print(a_list) # ๐๏ธ [0, 10, 20, 30, 40]`

The code for this article is available on GitHub

We used a
list comprehension to
iterate over the list and subtracted `5`

from each number.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we subtract `5`

from the current list item and return the
result.

Alternatively, you can use a simple `for`

loop.

`for`

loopThis is a four-step process:

- Declare a new variable that stores an empty list.
- Use a
`for`

loop to iterate over the original list. - On each iteration, subtract the number from the current item.
- Append the result to the new list.

main.py

`a_list = [5, 15, 25, 35, 45] new_list = [] for item in a_list: new_list.append(item - 5) print(new_list) # ๐๏ธ [0, 10, 20, 30, 40]`

The code for this article is available on GitHub

The

`for`

loop works in a very similar way to the list comprehension, but instead of returning the list items directly, we append them to a new list.The list.append() method adds an item to the end of the list.

If you use the `numpy`

module, you can also subtract the number directly from a
`numpy`

array.

main.py

`import numpy as np arr = np.array([5, 15, 25, 35, 45]) arr = arr - 5 print(arr) # ๐๏ธ [ 0 10 20 30 40] a_list = arr.tolist() print(a_list) # ๐๏ธ [0, 10, 20, 30, 40]`

The code for this article is available on GitHub

Make sure you have NumPy installed to be able to run the code sample.

shell

`pip install numpy # ๐๏ธ or with pip3 pip3 install numpy`

Subtracting a number from a `numpy`

array effectively subtracts the number from
each element in the array.

Note that this only works with `numpy`

arrays, you'd get a `TypeError`

if you
try to subtract a number from a native Python list.

The
tolist
method converts a `numpy`

array to a list.

Alternatively, you can use the `map()`

function.

`map()`

This is a three-step process:

- Pass a lambda function and the list to the
`map()`

function. - The lambda function should subtract the specific value from the provided list element.
- Use the
`list()`

class to convert the map object to a list.

main.py

`a_list = [5, 15, 25, 35, 45] new_list = list(map(lambda item: item - 5, a_list)) print(new_list) # ๐๏ธ [0, 10, 20, 30, 40]`

The code for this article is available on GitHub

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

The lambda function we passed to

`map`

gets called with each item in the list, subtracts `5`

from the item and returns the result.The last step is to use the `list()`

class to convert the `map`

object to a
`list`

.

Which approach you pick is a matter of personal preference. I'd use a list comprehension because I find them quite direct and easy to read.

`enumerate`

You can also use the `enumerate()`

function to subtract a value from every
number in a list.

main.py

`a_list = [5, 15, 25, 35, 45] for index, item in enumerate(a_list): a_list[index] = item - 5 # ๐๏ธ [0, 10, 20, 30, 40] print(a_list)`

The code for this article is available on GitHub

The enumerate() function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.

main.py

`my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): print(index, item) # ๐๏ธ 0 bobby, 1 hadz, 2 com`

On each iteration, we use the `index`

to assign to the current list item,
subtracting `5`

from its value.

I've also written an article on how to sum all values in a dictionary or list of dictionaries.

You can learn more about the related topics by checking out the following tutorials: