0
Python: How to divide list with another list?
Hi, a = [1, 2, 3, 4, 5] b = [10, 20, 30, 40, 50] z = b/a It seems like list cannot divide by another list. How do you divide b[0] by a[0]
6 Answers
+ 9
a = [1, 2, 3, 4, 5]
b = [10, 20, 30, 40, 50]
z = [m/n for m, n in zip(b, a)]
print(z) # [10.0, 10.0, 10.0, 10.0, 10.0]
+ 8
import numpy as np
a = np.array([10, 20, 30, 40, 50])
b = np.arange(1, 6)
print(a / b)
print(a / 10)
+ 6
print(*map(lambda x, y: x/y, b, a))
+ 5
a = [1,2,3,4,5]
b = [10,20,30,40,50]
c = [value/a[index] for index,value in enumerate(b)]
print(c)
+ 5
Wow so many solutions, thank you all
+ 4
z = map(lambda n1, n2: n1 / n2, b, a)