Solutions To Divide Each Element In A List By A Number In Python

Divide each element in a list by a number in Python

Today we will learn three ways to divide each element in a list by a number in Python. Let’s get started.

Divide each element in a list by a number in Python

In Python, we cannot directly divide a normal list by a number. For example, see the line of code below:

print([3, 6, 9] / 3)

Output:

TypeError: unsupported operand type(s) for /: 'list' and 'int'

Instead, follow these instructions:

Using for loop or list comprehension

The essence of for loop and list comprehension is to iterate over the elements of an iterable. But the syntax is different.

For Loop syntax:

for value in sequence:
    # Loop body

Parameters:

  • value: represents the element of the sequence on each iteration.
  • sequence: an iterable datatype such as a list, a tuple, a set, or a string.

List Comprehension syntax:

list = [expression for element in iterable]

Parameters:

  • element: the element of iterable on each iteration.
  • expression: any valid expression that returns a value.
  • iterable: an object that can be iterated upon, such as a List, a Tuple, a Set, or a string.

We can use the For Loop to divide each element in a list by a number in Python. Like this:

meters = [1200, 3400, 5600]

def m_to_km(meters):
  	result = []
    for meter in meters:
      	result.append(meter / 1000)
    return result

print(m_to_km(meters))

Output:

[1.2, 3.4, 5.6]

You can also use list comprehension to make it more concise. Like this:

meters = [1200, 3400, 5600]
kilometers = [meter / 1000 for meter in meters]
print(kilometers)

Output:

[1.2, 3.4, 5.6]

Using the map() functions and lambda function

map() function syntax:

map(function, iterable)

Parameters:

  • function: the function that is called on each iteration to execute for each item.
  • iterable: an object that can be iterated upon, such as a List, a Tuple, a Set, or a string.

lambda function syntax:

lambda arguments : expression

Description: 

The lambda is an anonymous function. It takes an argument and passes it to the expression to execute.

We use an anonymous lambda function to divide an element in the list by any number. The map() function takes each element of the list into a lambda function and returns a map object. Finally, we convert the returned result to a list to get the expected result.

meters = [1200, 3400, 5600]
kilometers = list(map(lambda meter : meter / 1000, meters))
print(kilometers)

Output:

[1.2, 3.4, 5.6]

Using division operator with NumPy

NumPy is a Python computation support library that stands for Numerical Python. To use NumPy, you must import NumPy into your application using this line of code:

Import numpy as np

To divide each element in a list by a number in Python. First, you must convert your list to an array. Then use the division operator to divide each element by a number. Then finally, you use the tolist() function to convert the returned result into a list.

import numpy as np

meters = [1200, 3400, 5600]
meters_arr = np.array(meters)
kilometers = meters_arr / 1000
print(kilometers.tolist())

Output:

[1.2, 3.4, 5.6]

Summary

There are many ways to divide each element in a list by a number in Python. However, three solutions mentioned in this article are the most straightforward approaches. Leave a comment below if you have any problems. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *