Home > Blockchain >  filter's arguments from two lists
filter's arguments from two lists

Time:09-19

I cant understand what my problem is. Can anybody help please?

lst1=[10,2,23,24,5,65,17,98,19,101]
lst2=[1,12,3,41,5,63,7,8,93,107]
Z=list(filter(lambda r,s : r==2 and s==12 , lst1,lst2))
print(Z)

I expect [2,12] but the output is: TypeError: filter expected 2 arguments, got 3

CodePudding user response:

The filter function only accepts one function and one iterator.

You can combine the lists via zip.

>>> list(zip(lst1,lst2))
[(10, 1), (2, 12), (23, 3), (24, 41), (5, 5), (65, 63), (17, 7), (98, 8), (19, 93), (101, 107)]

if you then iterate over every touple you can perform your filter like this

Z=list(filter(lambda r : r[0]==2 and r[1]==12 ,zip(lst1,lst2)))
>>> print(Z)
[(2, 12)]
  • Related