Sometimes we come across the situations in which we require to apply a particular function to each elements of two lists at similar index. Most popular of them are 4 of the elementary mathematics operations. These are quite similar and come up as application for certain utilities. Let’s discuss certain ways in which the division of two lists can be performed.
Method #1 : Using zip() + list comprehension
The zip operation can be used to link one list with the other and the computation part can be handled by the list comprehension and hence providing a shorthand to this particular problem.
# Python3 code to demonstrate # division of lists # using zip() + list comprehension # initializing lists test_list1 = [3, 5, 2, 6, 4] test_list2 = [7, 3, 4, 1, 5] # printing original lists print ("The original list 1 is : " + str(test_list1)) print ("The original list 2 is : " + str(test_list2)) # division of lists # using zip() + list comprehension res = [i / j for i, j in zip(test_list1, test_list2)] # printing result print ("The division list is : " + str(res)) |
The original list 1 is : [3, 5, 2, 6, 4] The original list 2 is : [7, 3, 4, 1, 5] The division list is : [0.42857142857142855, 1.6666666666666667, 0.5, 6.0, 0.8]
Method #2 : Using map()
Using map function is most elegant way in which we can possibly perform the twining of a function with both the lists. Different operations other than division can also be applied over it.
# Python3 code to demonstrate # division of lists # using map() from operator import truediv # initializing lists test_list1 = [3, 5, 2, 6, 4] test_list2 = [7, 3, 4, 1, 5] # printing original lists print ("The original list 1 is : " + str(test_list1)) print ("The original list 2 is : " + str(test_list2)) # division of lists # using map() res = list(map(truediv, test_list1, test_list2)) # printing result print ("The division list is : " + str(res)) |
The original list 1 is : [3, 5, 2, 6, 4] The original list 2 is : [7, 3, 4, 1, 5] The division list is : [0.42857142857142855, 1.6666666666666667, 0.5, 6.0, 0.8]
Recommended Posts:
- Python | Zipping two lists of lists
- Python | Program to count number of lists in a list of lists
- Python - Convert Lists into Similar key value lists
- Dividing a Large file into Separate Modules in C/C++, Java and Python
- Dividing Sticks Problem
- Creating a sorted merged list of two unsorted lists in Python
- Python | Intersection of two lists
- Python | Union of two or more Lists
- Python | Difference between two lists
- Python | Check if two lists have at-least one element common
- Python | Check whether two lists are circularly identical
- Python | Print all the common elements of two lists
- Python | Split the Even and Odd elements into two different lists
- Python | Find missing and additional values in two lists
- Python | Average of two lists
- Python | Ways to concatenate two lists
- Python | Check if two lists are identical
- Python | Convert two lists into a dictionary
- Python | Combining two sorted lists
- Python | Merge two lists into list of tuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


