Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python list filtering with arguments

Is there a way in python to call filter on a list where the filtering function has a number of arguments bound during the call. For example is there a way to do something like this:

>> def foo(a,b,c):     return a < b and b < c  >> myList = (1,2,3,4,5,6)  >> filter(foo(a=1,c=4),myList) >> (2,3) 

This is to say is there a way to call foo such that a=1, c=4, and b gets bound to the values in myList?

like image 615
kscottz Avatar asked Aug 12 '11 20:08

kscottz


People also ask

How do you filter a list based on a condition in Python?

Python Filter List with Condition. You can define any complicated condition on a list element to decide whether to filter it out or not. Just create your own function (e.g., condition(x) ) that takes one list element as input and returns the Boolean value True if the condition is met or False otherwise.

How do you filter a list of words in Python?

filter() method is a very useful method of Python. One or more data values can be filtered from any string or list or dictionary in Python by using filter() method. It filters data based on any particular condition. It stores data when the condition returns true and discard data when returns false.

What does the Filterfunction () take as a parameter?

The filter function takes two parameters: Function: Function that tests if elements of an iterable are True or false. If none, the function defaults to identity function returning false if any elements are false. Iterable: Iterable which is to be filtered could be sets, tuples, lists or containers of any iterators.

Can Python take list as argument?

You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.

How do you filter a list in Python?

Python Filter() Function. The filter() function accepts only two parameters. The first argument is the name of a user-defined function, and second is iterable like a list, string, set, tuple, etc. It calls the given function for every element of iterable, just like in a loop.

How does the filter () function work in Python?

The filter () function iterates over the elements of the list and applies the fn () function to each element. It returns an iterator for the elements where the fn () returns True. In fact, you can pass any iterable to the second argument of the filter () function, not just a list.

How do you pass a list as an argument in Python?

Passing a List as an Argument. You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function. E.g. if you send a List as an argument, it will still be a List when it reaches the function:

Can I call filter () without passing a function as the first argument?

Yes, you can call filter () without passing an actual function as the first argument. Instead, you can specify it as None. When None is specified in the filter (), then it pops out all elements that evaluate to False. Let’s consider the following list for illustration:


1 Answers

One approach is to use lambda:

>>> def foo(a, b, c): ...     return a < b and b < c ...  >>> myTuple = (1, 2, 3, 4, 5, 6) >>> filter(lambda x: foo(1, x, 4), myTuple) (2, 3) 

Another is to use partial:

>>> from functools import partial >>> filter(partial(foo, 1, c=4), myTuple) (2, 3) 
like image 184
senderle Avatar answered Oct 09 '22 03:10

senderle