In this tutorial, I am going to demonstrate different methods for dividing each element in a given list by an integer. You will see four methods to divide every element in a list by an integer. But first, let me give you a recap of Python lists.

## What is a list in Python?

A list is a data structure in Python which much similar to an array in C++ or Java. Python list is a mutable and ordered sequence of data values created by placing elements inside square brackets i.e. `[<insert elements seperated by commas]`

.

The elements inside a list can be directly accessed using their index values. Lists in Python are heterogenous, meaning they can have elements of various data types as opposed to languages like C++, Java which allows only one type of element in a list. A list can contain duplicates as well as None values.

**Examples of a Python list are:**

```
my_list = [8, 2, 35]
my_list = ['hello', 'world', 'python']
my_list = [15, 'hey', 54.25]
```

*Note: We know* *that a list can comprise int, float, strings, etc. but strings cannot be divided by an integer. Hence, all the elements in the list should be either int or float data types. *

*Also Read: Python List tutorials*

## 4 Ways to Divide Each Element in a Python List

Now let me show you different ways of dividing each element in a Python list by an integer.

### Method 1: Using a for loop

You can **create an empty list** to store the result and then iterate over the given list, divide each value in the list by the divisor and append the answer to the result list. Finally, print the resulting list.

```
sample_list = [15, 36, 45, 9, 14]
number = 3
result = []
for val in sample_list:
result.append(val/number)
print(result)
```

**Output:**

```
[5.0, 12.0, 15.0, 3.0, 4.666666666666667]
```

The output is computed as follows:

```
result[0] = sample_list[0]/number = 15/3 = 5
result[1] = sample_list[1]/number = 36/3 = 12
result[2] = sample_list[2]/number = 45/3 = 15
result[3] = sample_list[3]/number = 9/3 = 3
result[4] = sample_list[4]/number = 14/3 = 4.666666666666667
```

### Method 2: Using List Comprehension

List comprehension is a way of writing shorter or concise syntax for creating a new list based on the existing list. You can use list comprehension to divide each element in a list by an integer.

The algorithm for this is the same as the previous method using a for loop.

```
sample_list = [15, 36, 45, 9, 14]
number = 3
result = [val/number for val in sample_list]
print(result)
```

**Output:**

```
[5.0, 12.0, 15.0, 3.0, 4.666666666666667]
```

### Method 3: Keeping Reference to the Original List

If you wish to modify the existing list itself and do not want to create a new list to store the result, you can do so by keeping a reference to the original list as demonstrated below:

```
sample_list = [15, 36, 45, 9, 14]
number = 3
sample_list = [val/number for val in sample_list]
print(sample_list)
```

**Output:**

```
[5.0, 12.0, 15.0, 3.0, 4.666666666666667]
```

Here, the original list itself is updated on the go. Hence, the previous version of the list cannot be accessed anymore.

### Method 4: Using NumPy divide() function

NumPy, which stands for Numerical Python, is a Python library which supports a large number of mathematical operations. One such method is the numpy.divide() function. It takes two array-like objects as parameters and returns the resulting array by dividing the elements in the first array by the second one.

You can use this function to divide the elements in a list by an integer as shown in the below example:

```
import numpy as np
sample_list = [15, 36, 45, 9, 14]
number = 3
result = np.divide(sample_list, number)
print(result)
```

**Output:**

```
[ 5. 12. 15. 3. 4.66666667]
```

**Note:** The NumPy divide() function uses the ‘/’ operator to calculate the result. But in the other three methods, you can also use the floor divide i.e. the ‘//’ operator instead of the ‘/’ operator to get the floor of the result.

## Conclusion

In this tutorial, you learnt how to divide all the elements in a list by an integer using Python. You saw 4 different methods to achieve the result. The methods were as follows:

**Divide every element in a list by an integer using the following 4 methods.**

- Using a for loop
- Using List Comprehension
- Using List Comprehension while keeping the reference to the original list
- Using NumPy divide() function

All these methods return the same output as can be verified from the examples in this tutorial.

Please visit askpython.com for more such easy-to-understand Python tutorials.