How to Divide Each Element in a List by a Number in Python?

As a data scientist working with USA census data, I recently encountered a scenario where I needed to divide all the population counts in a list by a specific factor. Python provides several ways to accomplish this task efficiently. In this tutorial, I will explain how to divide each element in a list by a number in Python. Let us get into the different methods with practical examples.

Divide Each Element in a List by a Number in Python

To divide each element in a list by a number in Python, you can use a list comprehension. For example, if you have a list population_counts = [12345, 67890, 24680, 13579, 97531] and you want to divide each count by 1000, you can use the following code: normalized_counts = [count / 1000 for count in population_counts]. This will create a new list normalized_counts with the divided values: [12.345, 67.89, 24.68, 13.579, 97.531].

Suppose you have a list of population counts for various cities in the USA, and you want to divide each count by a certain number to normalize the data or perform some calculations. For example:

population_counts = [12345, 67890, 24680, 13579, 97531]
divisor = 1000

Our goal is to divide each population count by the divisor and create a new list with the updated values.

Read How to Find the Closest Value in a List Using Python?

Method 1: Use a For Loop

The most simple approach is to use a for loop to iterate over each element in the Python list and divide it by the desired number. Here’s an example:

population_counts = [12345, 67890, 24680, 13579, 97531]
divisor = 1000

normalized_counts = []
for count in population_counts:
    normalized_count = count / divisor
    normalized_counts.append(normalized_count)

print(normalized_counts)

Output:

[12.345, 67.89, 24.68, 13.579, 97.531]

You can see the output in the screenshot below.

Divide Each Element in a List by a Number in Python

In this method, we create an empty list called normalized_counts to store the divided values. We iterate over each count in the population_counts list, divide it by the divisor, and append the result to the normalized_counts list. Finally, we print the normalized_counts list.

Check out How to Sum Elements in a List in Python

Method 2: Use List Comprehension

List comprehension is a compact way to create a new Python list based on an existing list. It allows us to divide each element in the list by a number in a single line of code. Here’s how we can use list comprehension to solve our problem:

population_counts = [12345, 67890, 24680, 13579, 97531]
divisor = 1000

normalized_counts = [count / divisor for count in population_counts]

print(normalized_counts)

Output:

[12.345, 67.89, 24.68, 13.579, 97.531]

You can see the output in the screenshot below.

How to Divide Each Element in a List by a Number in Python

In this method, we create a new list called normalized_counts using list comprehension. The expression count / divisor is applied to each count in the population_counts list and the results are stored in the normalized_counts list.

Read How to Add Elements in List in Python using For Loop

Method 3: Use the map() Function

Another approach is to use the map() function in combination with a Python lambda function. The map() function applies a given function to each item in an iterable (such as a list) and returns a new iterable with the results. Here’s an example:

population_counts = [12345, 67890, 24680, 13579, 97531]
divisor = 1000

normalized_counts = list(map(lambda x: x / divisor, population_counts))

print(normalized_counts)

Output:

[12.345, 67.89, 24.68, 13.579, 97.531]

You can see the output in the screenshot below.

Divide Each Element in a List by a Number in Python map() function

In this method, we use the map() function and pass a lambda function lambda x: x / divisor as the first argument. The lambda function takes each element x from the population_counts list and divides it by the divisor. The map() function returns a new iterable with the divided values, which we convert to a list using the list() function.

Check out Compare Two Lists Python

Method 4: Use NumPy

If you’re working with large lists or arrays and need better performance, you can use the NumPy library. Python NumPy is a useful library for numerical computing in Python. It provides efficient array operations, including element-wise division. Here’s how you can use NumPy to divide each element in a list by a number:

import numpy as np

population_counts = [12345, 67890, 24680, 13579, 97531]
divisor = 1000

population_array = np.array(population_counts)
normalized_array = population_array / divisor

print(normalized_array)

Output:

[12.345 67.89  24.68  13.579 97.531]

In this method, we first convert the population_counts list to a NumPy array using np.array(). Then, we perform element-wise division by dividing the population_array by the divisor. NumPy automatically applies the division operation to each element in the array.

Read How to Find Mean of a List Python

Conclusion

In this tutorial, I helped you learn how to divide each element in a list by a number in Python. I discussed four important methods to accomplish this task such as using a for loop, using list comprehension, using the map() function, and using Numpy in Python.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.