Thursday, December 26, 2024
Google search engine
HomeLanguagesDivide all Elements of a List by a Number in Python

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




# Same list as the one used in the previous example
l = [14, 8, 0, 12, 981, 21, -99]
 
# The number to which all the elements of the list will be divided
divisor = 7
 
# Performing list comprehension and saving the
# result in a separate variable
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




# Same list as the one used in the previous example
l = [14, 8, 0, 12, 981, 21, -99]
 
# The number to which all the elements of the list will be divided
divisor = 7
 
# This list would store the quotient of the division
# operation for corresponding elements
final = []
 
# The loop iterates over each element of the list l
for x in l:
 
    # Each element of the list is divided by the divisor
    # and the result is stored in a separate list
    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




# Same list as the one used in the previous example
l = [14, 8, 0, 12, 981, 21, -99]
 
# The number to which all the elements of the list will be divided
divisor = 7
 
# This list would store the quotient of the division
# operation for corresponding elements
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
 
# A list containing integers
l = [14, 8, 0, 12, 981, 21, -99]
 
# Converting the list to an array and storing it in separate variable
arr = np.array(l)
 
# Number which will be used for elementwise division
divisor = 7
 
# Performing elementwise division operation on all elements of the array
result = arr/divisor
 
# Converting the array back to list
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
 
# Same list as the one used in the previous example
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: 

  1. Initialize a list l and a divisor.
  2. Create a Pandas series s from the list l.
  3. Apply a lambda function to the series s to divide each element of the series by the divisor.
  4. Convert the resulting series to a list using to_list() method.
  5. Print the resulting list.

Python3




import pandas as pd
 
# Same list as the one used in the previous example
l = [14, 8, 0, 12, 981, 21, -99]
 
# The number to which all the elements of the list will be divided
divisor = 7
 
# Creating a Pandas series from the list
s = pd.Series(l)
 
# Dividing each element of the series by the divisor
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




# Same list as the one used in the previous example
l = [14, 8, 0, 12, 981, 21, -99]
 
# The number to which all the elements of the list will be divided
divisor = 7
 
# Performing list comprehension with divmod() function and saving the
# result in a separate variable
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.

RELATED ARTICLES

Most Popular

Recent Comments