I have 3 lists and I need to calculate for each i the product of first list i, second list i and third list len-(i+1).
I tried to make a for loop which will do it for each i and I wrote this
JavaScript
x
6
1
i = 0
2
for i in list:
3
smh=list1[i]*list2[i]*list3[len-(i+1)]
4
print(smh)
5
i = i + 1
6
But it says in the second line “‘type’ object is not iterable” How can I make it work?
Advertisement
Answer
Here you would need to get a little fancy and use the zip()
function to properly iterate over all the members of the list.
here is some information on it: https://www.geeksforgeeks.org/python-iterate-multiple-lists-simultaneously/
JavaScript
1
7
1
list1 = [1,2]
2
list2 = [3,4]
3
list3 = [4,5]
4
for (a, b, c) in zip(list1, list2, reversed(list3)):
5
smh = a*b*c
6
print(smh)
7
Output: 15, 32.