Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple parameters for the __contains__ function

Is it possible to pass the "__ contains __" function in a list more than one parameter? I'd like to check if at least one of the items i have in a list exist in a different list.

For example: [0,1,4,8,87,6,4,7,5,'a','f','er','fa','vz']

I'd like to check if the one of the items (8,5,'f') are on that list.

How can I do it?

like image 463
iTayb Avatar asked Jun 29 '26 08:06

iTayb


1 Answers

AFAIK, __contains__ takes only one argument and it can't be changed.

However you can do following to get the desired result :

>>> a = [0,1,4,8,87,6,4,7,5,'a','f','er','fa','vz']
>>> any(map(lambda x: x in a, (8,5,'f')))
True

or

>>> from functools import partial
>>> from operator import contains
>>> f = partial(contains, a)
>>> any(map(f, (2,3)))
False
like image 65
Rumple Stiltskin Avatar answered Jun 30 '26 23:06

Rumple Stiltskin