views:

346

answers:

9

Consider this:

>>> a = [("one","two"), ("bad","good")]

>>> for i in a:
...     for x in i:
...         print x
... 
one
two
bad
good

How can I write this code, but using a syntax like:

for i in a:
    print [x for x in i]

Obviously, This does not work, it prints:

['one', 'two']
['bad', 'good']

I want the same output. Can it be done?

+4  A: 
import itertools
for item in itertools.chain(("one","two"), ("bad","good")):
    print item

will produce the desired output with just one for loop.

Tim Pietzcker
This works but I'm guessing (although not positive) that in his real code he has **n** number of tuples, not a predefined set that he can hardcode.
TM
To see what I'm getting at, read tgray's answer, with the `*a` notation.
TM
A: 

Not the best, but:

for i in a:
    some_function([x for x in i])

def some_function(args):
    for o in args:
        print o
unkiwii
+7  A: 

List comprehensions and generators are only designed to be used as expressions, while printing is a statement. While you can effect what you're trying to do by doing

from __future__ import print_function
for x in a:
    [print(each) for each in x]

doing so is amazingly unpythonic, and results in the generation of a list that you don't actually need. The best thing you could do would simply be to write the nested for loops in your original example.

Benjamin Pollack
It should also be noted that this will only work in Python 2.6 or 3
Jason Baker
much better off using the answer that **tgray** provided rather than this
TM
the print function really is awesome, however I think there is a much superior way to use it (hence my answer).
kaizer.se
+1  A: 

You'll need to define your own print method (or import __future__.print_function)

def pp(x): print x

for i in a:
  _ = [pp(x) for x in i]

Note the _ is used to indicate that the returned list is to be ignored.

notnoop
A: 

Not sure what the point is of omitting the inside loop.

You could do

for i, j in a:
   print i
   print j

Or

def printSet(aSet):
   for i in aSet: print i

for i in a:
   printSet(i)

If you just hate loops,

def printSet(aSet):
   for i in aSet: print i

map(printSet, a)

has the same side-effects, but also returns a list of Nones.

Inaimathi
+7  A: 

Given your example you could do something like this:

a = [("one","two"), ("bad","good")]

for x in sum(map(list, a), []):
    print x

This can, however, become quite slow once the list gets big.

The better way to do it would be like Tim Pietzcker suggested:

from itertools import chain

for x in chain(*a):
    print x

Using the star notation, *a, allows you to have n tuples in your list.

tgray
+1, star notation is the way to go for this.
TM
+3  A: 

The print function really is superior, but here is a much more pythonic suggestion inspired by Benjamin Pollack's answer:

from __future__ import print_function
for x in a:
    print(*x, sep="\n")

Simply use * to unpack the list x as arguments to the function, and use newline separators.

kaizer.se
+1  A: 

This code is straightforward and simpler than other solutions here:

for i in a:
    print '\n'.join([x for x in i])
Ofri Raviv
`print '\n'.join(j for k in a for j in k)`
stephan
+2  A: 
>>> a = [("one","two"), ("bad","good")]
>>> print "\n".join(j for i in a for j in i)
one
two
bad
good



>>> for i in a:
...  print "\n".join(i)
... 
one
two
bad
good
gnibbler