Divide all Elements of a List by a Number in Python
Performing mathematical operations on a list is quite common, as most newcomers to the language consider it a direct replacement for an array (due to its similar structure). Elementwise operations on such structures are performed to accomplish various tasks. In this article, you will learn how to divide all list elements by a number in Python with several methods, all of which perform the task at hand by dividing all the list elements by a number. The methods described will be:
- Using Python list comprehension
- Using Python for loops
- Using the Python map function
- Converting an array to perform elementwise division
- Using the Numpy divide method
Divide all Elements of a List by a Number in Python using list comprehension
List comprehension is a way of processing each element of a list without the overhead of running a loop on top of it. Then a variable result is used to store the result of list comprehension on each list element. List comprehension is performed using the inline loop constructor inside a list encloser and passing in the list as an argument. This traverses through each element of the list one at a time and performs the desired operation. In the end, the result is displayed.
Note: *The double slash operator “//” performs floor division on the elements for reasons explained earlier.
Python3
l = [ 14 , 8 , 0 , 12 , 981 , 21 , - 99 ]
divisor = 7
result = [x / / divisor for x in l]
print (result)
|
Output:
[2, 1, 0, 1, 140, 3, -15]
Time complexity: O(n)
Space complexity: O(n)
Divide all Elements of a List by a Number in Python using for Loop
The task could alternatively be accomplished by using Python for loops. The following code divides all elements of the list using a loop.
Python3
l = [ 14 , 8 , 0 , 12 , 981 , 21 , - 99 ]
divisor = 7
final = []
for x in l:
final.append(x / / divisor)
print (final)
|
Output:
[2, 1, 0, 1, 140, 3, -15]
Time complexity: O(n)
Space complexity: O(n)
Divide all Elements of a List by a Number in Python using the map
The same list and the divisor are used as in the previous examples. In a Python map, each element of the list is iterated over. In each iteration, the element is divided with the divisor and the result is stored in the new list.
Python3
l = [ 14 , 8 , 0 , 12 , 981 , 21 , - 99 ]
divisor = 7
final = list ( map ( lambda x: x / / divisor, l))
print (final)
|
Output:
[2, 1, 0, 1, 140, 3, -15]
Time complexity: O(n)
Space complexity: O(n)
Converting an array to perform elementwise division
Firstly NumPy is imported to allow array processing capabilities to Python. Then a list is initialized containing a list of numbers (integers). Then the list is converted to an array using the array method and passing the list as an argument. Then a variable containing the devisor is initialized with value 7. Then the elementwise division is performed on the array, and the operation result is stored in a variable. The array is converted back to a list using the tolist() function, and the resultant list is displayed.
Python3
import numpy as np
l = [ 14 , 8 , 0 , 12 , 981 , 21 , - 99 ]
arr = np.array(l)
divisor = 7
result = arr / divisor
l = result.tolist()
print (l)
|
Output:
[2.0, 1.1428571428571428, 0.0, 1.7142857142857142, 140.14285714285714, 3.0, -14.142857142857142]
The resultant values are in the floating point because of the use of the divisor operator for dividing the values, which results in floating point values. To overcome this, the double forward slash operator “//” could be used, which ignores the precision values upon division.
result = arr//divisor
Time complexity: O(n)
Space complexity: O(n)
Divide all Elements of a List by a Number in Python using Numpy
The task could alternatively be accomplished by using Numpy methods. The following code divides all elements of the list using the Numpy divide function. Below is the implementation of the code.
Python3
import numpy as np
l = [ 14 , 8 , 0 , 12 , 981 , 21 , - 99 ]
arr = np.array(l)
divisor = 7
res = (arr / / divisor).tolist()
divisor2 = 10
res2 = np.divide(arr, divisor2)
print (res)
print (res2)
|
Output:
[2, 1, 0, 1, 140, 3, -15]
[ 1.4 0.8 0. 1.2 98.1 2.1 -9.9]
Time complexity: O(n)
Space complexity: O(n)
Dividing List Elements by a Constant Using Pandas Series
Step by step Algorithm:
- Initialize a list l and a divisor.
- Create a Pandas series s from the list l.
- Apply a lambda function to the series s to divide each element of the series by the divisor.
- Convert the resulting series to a list using to_list() method.
- Print the resulting list.
Python3
import pandas as pd
l = [ 14 , 8 , 0 , 12 , 981 , 21 , - 99 ]
divisor = 7
s = pd.Series(l)
result = s. apply ( lambda x: x / / divisor)
print (result.to_list())
|
Output:
[2, 1, 0, 1, 140, 3, -15]
Time complexity: O(n), where n is the length of the list l. The Pandas apply() method has a time complexity of O(n), where n is the length of the series.
Auxiliary Space: O(n), where n is the length of the list l. The Pandas series s requires O(n) space. The result list also requires O(n) space.
Dividing List Elements by a Constant Using built-in divmod() function:
- Initialize a list l with some elements.
- Define a number divisor with which all the elements of the list will be divided.
- Use a list comprehension with divmod() function to divide all the elements of the list by the divisor and store the quotient in a new list named result.
- Print the result.
Python3
l = [ 14 , 8 , 0 , 12 , 981 , 21 , - 99 ]
divisor = 7
result = [ divmod (x, divisor)[ 0 ] for x in l]
print (result)
|
Output
[2, 1, 0, 1, 140, 3, -15]
The time complexity is O(n), where n is the number of elements in the list l, as we are performing a single pass over the list to perform the division.
The space complexity of this approach is also O(n), as we are storing the resulting list in memory.
Last Updated :
30 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...