Home > Mobile >  Compare each index of 3 lists and extract the smallest value of the 3, for every element in the 3 li
Compare each index of 3 lists and extract the smallest value of the 3, for every element in the 3 li

Time:12-17

I have 3 lists of 1000 elements each. I want to compare the values of each index across all 3 lists, take the smallest value of the 3, and put all 1000 of the smallest values in another list.

I've tried comparing each and every element from 2 lists using a for loop, but the loop only extracted the first index's value.

CodePudding user response:

Without numpy:

newlist = [ min(a,b,c) for a,b,c in zip(list1,list2,list3) ]

You need to use zip to interleave the elements.

CodePudding user response:

With out using zip function.

#Assuming length of list1,list2,list3 are same

list1=[1,2,3,0]
list2=[2,0,1,10]
list3=[-1,-1,10,-20]   

print([ min(list1[i],list2[i],list3[i]) for i in range(len(list1))])
  • Related