2

In python I have the following function:

def is_a_nice_element(element, parameter):
    #do something
    return True or False

Now I would like to filter a list with this function as predicate, giving a fixed parameter. Python has the itertools.ifilter function, but I can't figure out how to pass the parameter. Is this possible? If not, how can I solve this problem?

4 Answers 4

5

I like functools.partial much better than lambda.

itertools.ifilter( partial(is_a_nice_element, parameter=X), iterable )
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for using 'partial', although you didn't explained why you like it better.
3

Wrap it in a lambda:

itertools.ifilter(lambda e: is_a_nice_element(e, 42), iterable)

42 is your extra argument, or whatever else you want it to be.

Comments

1

The solutions use lambda and functools.partial are quite correct and directly answer your question, but I don't think they are really the ideal solution.

Using filter/itertools.ifilter/map/itertools.imap with an anonymous function is prettymuch always going to be less clear than using a generator expression or list comprehension. For example, in this case I would write the generator expression

(item for item in iterable if is_a_nice_element(item, constant))

instead of using ifilter. In this case the ifilter solution is still fairly readable, but in many cases trying to go through lots of trouble to define just the right function is not going to be as worth while as just performing the operation.

1 Comment

A disadvantage of doing it this way is that you can't parametrize the filter. EG if you want to do this filtering within a function like def find_nice_elements(filter_function=partial(is_nice, p=42)): ...
-1

If you are using ifilter then parameter would need to be constant in which case you could use a default argument parameter=something. If you want parameter to vary, you'd need to use another method to take a dyadic predicate.

If you already have the list in hand, ifilter is a bit of overkill relative to the built-in filter.

Describing what you want to accomplish would help in making a more concrete answer.

1 Comment

Sorry, but I need this parameter to be given there. If I wouldn't need the parameter there I wouldn't have a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.