3

I'm trying to figure out the syntax for passing arguments from one list or dict to another in the for loop syntax.

The desired result I'm looking for is this:

for bean in beans:
  if bean.type == 'coffee':
    print bean

Only, instead of printing to stdout, I'd like to collect that string data and append it to another list. Eventually flattening the list.

The kicker, I want to perform this in a single line.

I know of the ''.join() method, I'm looking for this result so I can filter the results from the for-in loop.

3 Answers 3

12
[bean for bean in beans if bean.type == 'coffee']

List comprehensions are neat. Even neater, often you don't need to produce a whole list - you just need an iterator that gives you the values the list would consist of. That's a generator, and they can be expressed just as succinctly via generator expressions. Those are written in the same way as list comprehensions except that the square brackets become parens (and you can omit them if it's the only argument in a function call) e.g. '\n'.join(str(bean) for bean in beans if bean.type == 'coffee'). The advantage is the mentioned laziness, i.e. you never generate values until they're really needed (requested) and you don't keep all of them in memory at the same time (unless of course the consumer does this).

You can use itertools.chain to chain multiple iterables (including lists) to one, or if you can't change the fact you're getting lists of lists, you can use (x for list in lists for x in list). For a generalized solution involving abritary deep nesting, you need a full function utilizing recursion.

Sign up to request clarification or add additional context in comments.

3 Comments

Since list is a built-in, one may wish to avoid use of a variable with the same name.
@Adeel: Generally true (and I'll edit if you make a good suggestion - I couldn't think of any generic name except the very cryptic xs). Less of an issue here though, since generator expressions don't leak the iteration variables to the outside (anymore).
You're absolutely right about Python 3 fixing leakage of loop control variables in list comprehensions. Only those of us still using Python 2 have to worry about this issue.
8

A one liner would use list or generator comprehensions, see Blair's answer.

An adaption of your current code would suit the yield keyword, which allows you to construct a generator function like this:

def coffee_filter(beans):
    for bean in beans:
       if bean.type == 'coffee':
           yield bean

for bean in coffee_filter(beans):
    print "coffee from %s" % bean.country

Since python allows you to define functions pretty much anywhere, this is really useful.

4 Comments

Writing a full generator is overkill in 90% of all cases. This is no exception. If a generator expression works, just go that way.
I'm a little surprised you didn't use a generator expression as the innards of your method. I'm not recommending getting rid of the function - if nothing else it provides documentation by virtue of its name (which is cute, and would've earned you a +1 even if your answer were of lower quality...)
@Blair: Names are easy enough to add (and a good idea indeed): Just define filtered_coffee = <genexpr> and use that instead of inlining it ;)
@delnan @Blair: I read only part of the question before writing the post, and missed the one-liner part, so I just transformed the code to yield instead of print. (I added the first sentence in an edit afterwards.)
1
'\n'.join([str(bean) for bean in beans if bean.type == 'coffee'])

3 Comments

You could skip the [] and get a generator expression instead. This avoids building the intermediate list.
@Macke, Fair enough. I'm away from a Python interpreter and didn't want to risk str.join() not taking an iterable - I've been bitten by things wanting lists before. Then again, that's probably .Net polluting my brain...
Rest assured that in Python, everything that possibly can accept abritary iterables will. Unless of course written by a poor soul who doesn't know who outragously pointless for i in range(len(list)) is...

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.