# Dividing two lists in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

The elements in tow lists can be involved in a division operation for some data manipulation activity using python. In this article we will see how this can be achieved.

## With zip

The zip function can pair up the two given lists element wise. The we apply the division mathematical operator to each pair of these elements. Storing the result into a new list.

## Example

Live Demo

# Given lists
list1 = [12,4,0,24]
list2 = [6,3,8,-3]

# Given lists
print("Given list 1 : " + str(list1))
print("Given list 2 : " + str(list2))

# Use zip
res = [i / j for i, j in zip(list1, list2)]

print(res)

## Output

Running the above code gives us the following result −

Given list 1 : [12, 4, 0, 24]
Given list 2 : [6, 3, 8, -3]
[2.0, 1.3333333333333333, 0.0, -8.0]

## With truediv and map

The truediv operator is a part of python standard library called operator. It performs the true division between the numbers. We also use the map function to apply the division operator iteratively for each pair of elements in the list.

## Example

Live Demo

from operator import truediv
# Given lists
list1 = [12,4,0,24]
list2 = [6,3,8,-3]

# Given lists
print("Given list 1 : " + str(list1))
print("Given list 2 : " + str(list2))

# Use zip
res = list(map(truediv, list1, list2))

print(res)

## Output

Running the above code gives us the following result −

Given list 1 : [12, 4, 0, 24]
Given list 2 : [6, 3, 8, -3]
[2.0, 1.3333333333333333, 0.0, -8.0]
Updated on 05-May-2020 10:06:59