Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

filter's arguments from two lists

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading