Divide all Elements of a List by a Number in Python
Last Updated :
03 Feb, 2025
The task of dividing all elements of a list by a number in Python involves iterating over the list and applying the division operation to each element. For example, given a list a = [14, 8, 0, 12, 981, 21, -99] and a divisor d = 7, the result after dividing each element by 7 will be [2, 1, 0, 1, 140, 3, -14].
Using list comprehension
List comprehension is one of the most efficient way to transform elements of a list. It provides a concise syntax for iterating over the list and performing operations, such as dividing each element by a number. This method creates a new list by applying the operation to each element in a single, readable line of code.
Python
a = [14, 8, 0, 12, 981, 21, -99]
d = 7 # divisor
res = [x // d for x in a]
print(res)
Output[2, 1, 0, 1, 140, 3, -15]
Explanation: list comprehension iterate over each element x in the list a and performs integer division // of each element by d . The results are collected into a new list res .
Using map()
When dividing all elements of a list by a number, map() can be combined with a lambda function for an efficient transformation. This method avoids the overhead of explicitly iterating over the list using a loop, but it can be slightly less efficient than list comprehension, especially for simple operations.
Python
a = [14, 8, 0, 12, 981, 21, -99]
d = 7
res = list(map(lambda x: x // d, a))
print(res)
Output[2, 1, 0, 1, 140, 3, -15]
Explanation: map() apply integer division x // d to each element in the list a using a lambda function, and then converts the result into a list with list().
Using for loop
In this method, we explicitly iterate over each item in the list and perform the division operation, appending the result to a new list. While this method works fine for smaller lists, it introduces additional overhead with each loop iteration and append() operation, making it less efficient for larger datasets.
Python
a = [14, 8, 0, 12, 981, 21, -99]
d = 7 # divisor
res = [] # initialize empty list
for x in a:
res.append(x // d)
print(res)
Output[2, 1, 0, 1, 140, 3, -15]
Explanation: for loop iterate over each element x in the list a. For each element, it performs integer division x // d , dividing x by d and appends the result to the res list.
Using Numpy
When working with large datasets, using NumPy for dividing all elements of a list by a number can be highly efficient, as NumPy performs operations using vectorization techniques. This allows us to apply the division operation across the entire array in a single, highly optimized step, reducing the computational overhead compared to lists.
Python
import numpy as np
a = [14, 8, 0, 12, 981, 21, -99]
d = 7 # divisor
a_np = np.array(a) # Convert `a` into NumPy array
res = a_np // d
print(res)
Output[ 2 1 0 1 140 3 -15]
Explanation: This code imports NumPy, converts the list a into a NumPy array a_np and performs integer division // of each element by the divisor d. The result is stored in res .
Similar Reads
Find Average of a Number Digits in Python AIn this article, we will see how to find the average of the digits of a number in Python. Examples: Input: N = 642 Output: 4.0 Explanation: (6+4+2)/3 = 12/3 = 4.0 Input: N = 3504 Output: 3.0 Explanation: (3+5+0+4)/4 = 12/4 = 3.0Calculate the average of a number of digits in python if the number is
3 min read
Multiply All Numbers in the List in Python Our task is Multiplying all numbers in a list Using Python. This can be useful in calculations, data analysis, and whenever we need a cumulative product. In this article we are going to explore various method to do this. Using a loopWe can simply use a loop (for loop) to iterate over the list elemen
2 min read
Find average of a list in python We are given a list of numbers and the task is to find the average (or mean) value of its elements. For example, if we have a list nums = [10, 20, 30, 40], the sum of the elements is 10 + 20 + 30 + 40, which equals 100. The number of elements in the list is 4. So, the average is calculated as 100 /
2 min read
Compute the Reciprocal for all elements in a NumPy array In this article, let's discuss how to compute the reciprocal for all the elements of a given NumPy array. Method 1: Through reciprocal_arr = 1/arr  statement, we can convert every element of arr to it reciprocal and saved it to reciprocal_arr.But there is a catch, you will be encountered with an err
2 min read
Divide each row by a vector element using NumPy The following article depicts how to Divide each row by a vector element using NumPy. The vector element can be a single element, multiple element, or array. The division operator ( / ) is employed to produce the required functionality. Â We can divide rows of 1-D, 2-D, or even more types of arrays w
2 min read