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.

Shaido
  • 27,497
  • 23
  • 70
  • 73
jbcurtin
  • 1,793
  • 2
  • 14
  • 23

3 Answers3

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.

  • 1
    Since list is a built-in, one may wish to avoid use of a variable with the same name. – Adeel Zafar Soomro Feb 27 '11 at 20:04
  • 1
    @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). –  Feb 27 '11 at 20:53
  • 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. – Adeel Zafar Soomro Feb 28 '11 at 21:06
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.

Community
  • 1
  • 1
Macke
  • 24,812
  • 7
  • 82
  • 118
  • 2
    Writing a full generator is overkill in 90% of all cases. This is no exception. If a generator expression works, just go that way. –  Feb 27 '11 at 19:17
  • 1
    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 Conrad Feb 27 '11 at 19:17
  • @Blair: Names are easy enough to add (and a good idea indeed): Just define `filtered_coffee = ` and use that instead of inlining it ;) –  Feb 27 '11 at 19:21
  • @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.) – Macke Feb 27 '11 at 20:06
1
'\n'.join([str(bean) for bean in beans if bean.type == 'coffee'])
Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
  • 1
    You could skip the [] and get a generator expression instead. This avoids building the intermediate list. – Macke Feb 27 '11 at 19:10
  • @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... – Blair Conrad Feb 27 '11 at 19:14
  • 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... –  Feb 27 '11 at 19:16