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
- Related Questions & Answers
- Adding two Python lists elements
- Merge Two Sorted Lists in Python
- Combining two sorted lists in Python
- Intersection of Two Linked Lists in Python
- Concatenate two lists element-wise in Python
- How to compare two lists in Python?
- How do we compare two lists in Python?
- Check if two lists are identical in Python
- Difference of two lists including duplicates in Python
- Convert two lists into a dictionary in Python
- Intersect two lists in C#
- Python program to find Intersection of two lists?
- In Python how to create dictionary from two lists?
- Program to find minimum difference between two elements from two lists in Python
- How to map two lists into a dictionary in Python?
Dividing values of dictionaries by values of another dictionary python
Questions : Dividing values of dictionaries by values of another dictionary python
2022-09-17T19:38:58+00:00 2022-09-17T19:38:58+00:00
823
I have a dictionary, dict1, in the following anycodings_division format:
{0: 301, 1: 410, 2: 289}I have another dictionary, dict2:
{0: 5307, 2: 4925}I would like to divide the value of dict2 by anycodings_division the value of dict1, based on the keys. For anycodings_division example 5307 would be divided by 301 since anycodings_division their keys are both 0. Any suggestions?
Total Answers 3
32
Answers 1 : of Dividing values of dictionaries by values of another dictionary python
>>> d1 = {0: 301, 1: 410, 2: 289} >>> d2 = {0: 5307, 2: 4925} >>> {k: d2[k] / float(d1[k]) for k in d1 if k in d2} {0: 17.631229235880397, 2: 17.041522491349482}
If your Python (eg 2.6) is too old for anycodings_division dict comprehensions, you can use
>>> dict((k, d2[k] / float(d1[k])) for k in d1 if k in d2) {0: 17.631229235880397, 2: 17.041522491349482}If your Python is even older than 2.6
>>> dict([(k, d2[k] / float(d1[k])) for k in d1 if k in d2]) {0: 17.631229235880397, 2: 17.041522491349482}In Python3, you don't need the float anycodings_division call
0
2022-09-17T19:38:58+00:00 2022-09-17T19:38:58+00:00Answer Link
mRahman
2
Answers 2 : of Dividing values of dictionaries by values of another dictionary python
def divide(dividends, divisors): ret = dict() for key, dividend in dividends.items(): ret[key] = dividend/divisors.get(key, 1) return ret
0
2022-09-17T19:38:58+00:00 2022-09-17T19:38:58+00:00Answer Link
joy
4
Answers 3 : of Dividing values of dictionaries by values of another dictionary python
Loop over every item the length of the anycodings_division smallest dict. Then add the values to anycodings_division another dict, values.
>>> dict1 = {0: 301, 1: 410, 2: 289} >>> >>> dict2 = {0: 5307, 1: 4925} >>> >>> length = {len(dict1): 'dict1', len(dict2): 'dict2'} >>> small = min(length) >>> values = {} >>> >>> for k in range(small): ... values[k] = dict1[k]*dict2[k] ... >>> print values {0: 1597407, 1: 2019250} >>>0
2022-09-17T19:38:58+00:00 2022-09-17T19:38:58+00:00Answer Link
jidam