Open In App

Python - Dividing two lists

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Dividing two lists in Python means performing division between elements at the same position in both lists. Each element in the first list is divided by the corresponding element in the second list to create a new list of results. In this article, we will explore How to Divide two lists.

Using NumPy

NumPy provides fast, memory-efficient element-wise division, making it ideal for large datasets and numerical computations. It requires importing the NumPy library but offers high performance for array operations.

Python
import numpy as np
a = [10, 20, 30]
b = [2, 4, 5]

# Convert lists to NumPy arrays 
# perform element-wise division
res = np.array(a) / np.array(b)
print(res)

Output
[5. 5. 6.]

Explanation:

  • np.array(a) and np.array(b) convert the lists a and b into NumPy arrays.
  • The division is performed element-wise, and the result is a NumPy array containing the results of each division..

Let's discuss more method to dividing two lists.

Using List Comprehension with zip()

Using list comprehension with zip() allows for easy element-wise division of two lists by pairing their elements. It’s a concise and efficient solution for moderate-sized lists without needing external libraries.

Python
a = [10, 20, 30]
b = [2, 4, 5]

# Use list comprehension with zip to divide elements
res = [x / y for x, y in zip(a, b)]
print(res)

Output
[5.0, 5.0, 6.0]

Explanation:

  • zip(a, b) pairs corresponding elements from a and b.
  • The list comprehension divides each pair of elements and stores the results in the list res.

Using Loop

Using loop with zip() allows for more control over the iteration and division process, making it suitable for adding custom logic. It is longer than list comprehension but offers greater flexibility.

Python
a = [10, 20, 30]
b = [2, 4, 5]

# Initialize result list
res = []

# Use a loop with zip to divide corresponding elements
for a, b in zip(a, b): 
    res.append(a / b)
print(res)

Output
[5.0, 5.0, 6.0]

Explanation:

  • zip(a, b) pairs corresponding elements from a and b.
  • The loop divides each pair of elements and appends the result to the list res.

Using map()

map() function apply division to corresponding elements of two lists. It's a concise, functional approach but can be less readable for complex tasks.

Example:

Python
a = [10, 20, 30]
b = [2, 4, 5]

# Use map() to apply a lambda function 
#that divides corresponding elements of a and b
res = list(map(lambda a, b: a / b, a, b))
print(res)

Output
[5.0, 5.0, 6.0]

Explanation:

  • map() divides corresponding elements from a and b.
  • Results are stored in res and printed.

Similar Reads