-1

When I'm changing output type between list and tuple, one is showing output whereas other is just showing location of result. Why does this happen?

>>> symbol = '123456789'
>>> (s for s in symbol)
<generator object <genexpr> at 0x7fc0e9e2fbf8>
>>> [s for s in symbol]
['1', '2', '3', '4', '5', '6', '7', '8', '9']
  • @NedBatchelder And google'ing 'generator object' probably would have sufficed instead of making a post. – dfundako Jul 07 '18 at 14:50
  • 2
    Possible duplicate of [Why is there no tuple comprehension in Python?](https://stackoverflow.com/questions/16940293/why-is-there-no-tuple-comprehension-in-python) – Brad Solomon Jul 07 '18 at 14:50

1 Answers1

-1

(s for s in symbol) is generator comprehension. Similar to:

symbol = '123456789'
def func():
    for s in symbol:
        yield s

g = func()
print(g)
<generator object func at 0x7ffff7e59410>

print(tuple(g)) 
('1', '2', '3', '4', '5', '6', '7', '8', '9')

If you supply the generator to list() or tuple(), the generator will run.

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91