2

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?

2
  • 1
    I'm not quite clear what you're trying to do. Your second example has an implicit loop via a list generator. Is your issue simply that you don't want to have two for loops following each other for aesthetic reasons? Commented Nov 30, 2009 at 18:23
  • 5
    Personally, I think the loop you already have is the best way to do this. It's simple, clear and readable. Commented Nov 30, 2009 at 18:42

8 Answers 8

7

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.

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

3 Comments

It should also be noted that this will only work in Python 2.6 or 3
much better off using the answer that tgray provided rather than this
the print function really is awesome, however I think there is a much superior way to use it (hence my answer).
7

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.

Comments

4
>>> 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

Comments

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

will produce the desired output with just one for loop.

2 Comments

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.
To see what I'm getting at, read tgray's answer, with the *a notation.
3

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.

Comments

1

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.

Comments

1

This code is straightforward and simpler than other solutions here:

for i in a:
    print '\n'.join([x for x in i])

1 Comment

print '\n'.join(j for k in a for j in k)
0

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

Comments

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.