147

In the Python shell, if I enter a list comprehension such as:

>>> [x for x in string.letters if x in [y for y in "BigMan on campus"]]

I get a nicely printed result:

['a', 'c', 'g', 'i', 'm', 'n', 'o', 'p', 's', 'u', 'B', 'M']

Same for a dictionary comprehension:

>>> {x:x*2 for x in range(1,10)}
{1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}

If I enter a generator expression, I get not such a friendly response:

>>> (x for x in string.letters if x in (y for y in "BigMan on campus"))
<generator object <genexpr> at 0x1004a0be0>

I know I can do this:

>>> for i in _: print i,
a c g i m n o p s u B M

Other than that (or writing a helper function) can I easily evaluate and print that generator object in the interactive shell?

martineau
  • 119,623
  • 25
  • 170
  • 301
the wolf
  • 34,510
  • 13
  • 53
  • 71
  • 2
    What is the real problem here? What are you missing? –  Mar 02 '11 at 07:35
  • 5
    @pynator: The "real problem" is just that I want to be able to print the content of `generator object` as I interactively build a comprehension at the interactive prompt. Calling `list(_)` does that. What I have done is used list comprehensions then turn those into genexp in larger code. These can fail at run time in ways that list comprehensions do not. – the wolf Mar 02 '11 at 07:42
  • 9
    The short answer is that a generator expression cannot be printed because its values don't exist; they're generated on demand. What you can do (assuming the generator stops sometime) is get all the values out of it, like with `list()`, and then print them. – Kos May 26 '13 at 19:46
  • Another way to do it is ```x = (i for i in range(1)); print(*x)``` – kendfss Nov 21 '20 at 00:19

8 Answers8

208

Quick answer:

Doing list() around a generator expression is (almost) exactly equivalent to having [] brackets around it. So yeah, you can do

>>> list((x for x in string.letters if x in (y for y in "BigMan on campus")))

But you can just as well do

>>> [x for x in string.letters if x in (y for y in "BigMan on campus")]

Yes, that will turn the generator expression into a list comprehension. It's the same thing and calling list() on it. So the way to make a generator expression into a list is to put brackets around it.

Detailed explanation:

A generator expression is a "naked" for expression. Like so:

x*x for x in range(10)

Now, you can't stick that on a line by itself, you'll get a syntax error. But you can put parenthesis around it.

>>> (x*x for x in range(10))
<generator object <genexpr> at 0xb7485464>

This is sometimes called a generator comprehension, although I think the official name still is generator expression, there isn't really any difference, the parenthesis are only there to make the syntax valid. You do not need them if you are passing it in as the only parameter to a function for example:

>>> sorted(x*x for x in range(10))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Basically all the other comprehensions available in Python 3 and Python 2.7 is just syntactic sugar around a generator expression. Set comprehensions:

>>> {x*x for x in range(10)}
{0, 1, 4, 81, 64, 9, 16, 49, 25, 36}

>>> set(x*x for x in range(10))
{0, 1, 4, 81, 64, 9, 16, 49, 25, 36}

Dict comprehensions:

>>> dict((x, x*x) for x in range(10))
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

>>> {x: x*x for x in range(10)}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

And list comprehensions under Python 3:

>>> list(x*x for x in range(10))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

>>> [x*x for x in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Under Python 2, list comprehensions is not just syntactic sugar. But the only difference is that x will under Python 2 leak into the namespace.

>>> x
9

While under Python 3 you'll get

>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

This means that the best way to get a nice printout of the content of your generator expression in Python is to make a list comprehension out of it! However, this will obviously not work if you already have a generator object. Doing that will just make a list of one generator:

>>> foo = (x*x for x in range(10))
>>> [foo]
[<generator object <genexpr> at 0xb7559504>]

In that case you will need to call list():

>>> list(foo)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Although this works, but is kinda stupid:

>>> [x for x in foo]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • 6
    The official term remains "generator expression" because the word "comprehension" implies iteration, which is one thing a genexp *doesn't* do, as this question and answer illustrate nicely :) – ncoghlan Mar 02 '11 at 15:02
  • 3
    `list( generator-expression )` isn't printing the generator expression; it is generating a list (and then printing it in an interactive shell). Instead of generating a list, in Python 3, you could splat the generator expression into a print statement. Ie) `print(*(generator-expression))`. This prints the elements without commas and without brackets at the beginning and end. – AJNeufeld Jul 20 '18 at 16:47
22

Or you can always map over an iterator, without the need to build an intermediate list:

>>> _ = map(sys.stdout.write, (x for x in string.letters if x in (y for y in "BigMan on campus")))
acgimnopsuBM
lbolla
  • 5,387
  • 1
  • 22
  • 35
  • 7
    this is the only answer which actually prints contents of generator without creating huge object. – Marek R Apr 05 '19 at 09:08
20

Unlike a list or a dictionary, a generator can be infinite. Doing this wouldn't work:

def gen():
    x = 0
    while True:
        yield x
        x += 1
g1 = gen()
list(g1)   # never ends

Also, reading a generator changes it, so there's not a perfect way to view it. To see a sample of the generator's output, you could do

g1 = gen()
[g1.next() for i in range(10)]
chad
  • 1,339
  • 12
  • 10
19

You can just wrap the expression in a call to list:

>>> list(x for x in string.letters if x in (y for y in "BigMan on campus"))
['a', 'c', 'g', 'i', 'm', 'n', 'o', 'p', 's', 'u', 'B', 'M']
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
4

Generator object do not store actual data, it is basically just an expression. Program can not print what will be value of an expression without evaluating it. Generator object(generator expression) can be evaluated by typecasting into any iterable data type.

eg.

list(genexpr)
dict(genexpr)
set(genexpr)
for data in genexpr: 

Additional

generating generator expression and then typecasting is 20% slower than directly creating required datatype object. So if we require whole data it's better to use

data=[x for x in range(0,10)]

than using

genexpr=(x for x in range(0,10))
data=list(genexpr)
3
>>> list(x for x in string.letters if x in (y for y in "BigMan on campus"))
['a', 'c', 'g', 'i', 'm', 'n', 'o', 'p', 's', 'u', 'B', 'M']
3

You could also just do something like this:

gen = (i for i in 'abcde')
print( *gen ) # => a b c d e
drvnprgrmr
  • 41
  • 4
2
print(i for i in range(9))

if we run this we will get output as: - <generator object at 0x000001F01A153E40>

one simple way to print generator is converting it to list. so simply if we modify our code as print(*[i for i in range(9)])

So we will get output as : 0 1 2 3 4 5 6 7 8

  • 3
    Why do you need the `*` operator ( it would print the list without it as well) – mfit Feb 25 '22 at 19:49
  • 2
    @mfit `Why do you need the * operator`: to be equivalent to `for i in _: print i`, the star is required else you get `_`. – mins Nov 07 '22 at 10:17