
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Dividing two lists in Python
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
# 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
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]
- Related Articles
- Merge Two Sorted Lists in Python
- Combining two sorted lists in Python
- Adding two Python lists elements
- Intersection of Two Linked Lists in Python
- How to compare two lists in Python?
- Concatenate two lists element-wise in Python
- How do we compare two lists in Python?
- Difference of two lists including duplicates in Python
- Check if two lists are identical in Python
- Convert two lists into a dictionary in Python
- In Python how to create dictionary from two lists?
- Python program to find Intersection of two lists?
- Python – Cross mapping of Two dictionary value lists
- How to map two lists into a dictionary in Python?
- Python - Check if two lists have any element in common

Advertisements