# Divide each element in a list by a number in Python Last updated: Jul 11, 2022 Photo from Unsplash

## Divide each element in a list by a number in Python#

To divide each element in a list by a number:

1. Use a list comprehension to iterate over the list.
2. On each iteration, divide the current list element by the number.
3. The new list will contain the results of the division.
main.py
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```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.

## Divide each element in a list by a number using for loop#

To divide each element in a list by a number:

1. Declare a new variable that stores an empty list.
2. Use a `for` loop to iterate over the original list.
3. On each iteration, divide the current list item by the number.
4. Append the result to the new list.
main.py
```Copied!```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
```Copied!```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`.

## Divide each element in a list by a number using Numpy#

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

main.py
```Copied!```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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer. You can use the search field on my Home Page to filter through all of my articles.