Divide each element in a list by a number in Python

Borislav Hadzhiev

Last updated: Jul 11, 2022

Photo from Unsplash

**To divide each element in a list by a number:**

- Use a list comprehension to iterate over the list.
- On each iteration, divide the current list element by the number.
- The new list will contain the results of the division.

main.py

`my_list = [8, 12, 20] # ✅ divide each element in list by number new_list = [item / 2 for item in my_list] print(new_list) # 👉️ [4.0, 6.0, 10.0] # -------------------------------------- # ✅ divide each element in list by number using floor division new_list_2 = [item // 2 for item in my_list] print(new_list_2) # 👉️ [4, 6, 10]`

We used a list comprehension to iterate over the list and divide each list item
by `2`

.

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 divide the current list item by the specified number and return the result.

If you need to divide each element in a list by a number without a remainder,
use the floor division `//`

operator.

main.py

`my_list = [8, 12, 20] new_list_2 = [item // 2 for item in my_list] print(new_list_2) # 👉️ [4, 6, 10]`

Division `/`

of integers yields a float, while floor division `//`

of integers
results in an integer.

The result of using the floor division operator is that of a mathematical
division with the `floor()`

function applied to the result.

main.py

`my_num = 60 print(my_num / 10) # 👉️ 6.0 (float) print(my_num // 10) # 👉️ 6 (int)`

Using floor division is like applying the math.floor method to the result.

The method returns the largest integer less than or equal to the provided number.

main.py

`import math result_1 = math.floor(25 / 4) print(result_1) # 👉️ 6 result_2 = 25 / 4 print(result_2) # 👉️ 6.25`

Alternatively, you can use a `for`

loop.

**To divide each element in a list by a number:**

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

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

main.py

`my_list = [20, 30, 60] new_list = [] for item in my_list: new_list.append(item / 2) print(new_list) # 👉️ [10.0, 15.0, 30.0]`

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.You can also use the `map()`

function to divide each element in a list by a
number.

main.py

`my_list = [20, 30, 60] new_list = list(map(lambda item: item / 2, my_list)) print(new_list) # 👉️ [10.0, 15.0, 30.0]`

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, divides the item by `2`

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

class to convert the `map`

object to a
`list`

.

You can divide each element in a numpy array by a number by using the division operator.

main.py

`import numpy as np arr = np.array([10, 25, 35, 66]) new_arr_1 = arr / 2 print(new_arr_1) # 👉️ [ 5. 12.5 17.5 33. ]`

Dividing a numpy array by a number effectively divides each element in the array by the specified number.

Note that this only works with numpy arrays. If you divide a Python list by a number, you'd get an error.