Questions tagged [generator-expression]

A syntactic construct in Python providing a concise way to create a generator object, with syntax similar to a list comprehension.

Quoting the Python generator expression reference:

A generator expression is a compact generator notation in parentheses:

generator_expression ::=  "(" expression comp_for ")"

A generator expression yields a new generator object. Its syntax is the same as for comprehensions, except that it is enclosed in parentheses instead of brackets or curly braces.

148 questions
520
votes
13 answers

Generator expressions vs. list comprehensions

When should you use generator expressions and when should you use list comprehensions in Python? # Generator expression (x*2 for x in range(256)) # List comprehension [x*2 for x in range(256)]
readonly
  • 343,444
  • 107
  • 203
  • 205
130
votes
5 answers

How does this input work with the Python 'any' function?

In the python docs page for any, the equivalent code for the any() function is given as: def any(iterable): for element in iterable: if element: return True return False How does this function know what element I wanna…
pythoniku
  • 3,532
  • 6
  • 23
  • 30
83
votes
1 answer

Generator as function argument

Can anyone explain why passing a generator as the only positional argument to a function seems to have special rules? If we have: def f(*args): print "Success!" print args This works, as expected. >>> f(1, *[2]) Success! (1, 2) This does…
DeTeReR
  • 829
  • 1
  • 6
  • 12
80
votes
1 answer

yield in list comprehensions and generator expressions

The following behaviour seems rather counterintuitive to me (Python 3.4): >>> [(yield i) for i in range(3)] at 0x0245C148> >>> list([(yield i) for i in range(3)]) [0, 1, 2] >>> list((yield i) for i in range(3)) [0, None,…
zabolekar
  • 1,624
  • 1
  • 12
  • 17
59
votes
8 answers

Unexpected behaviour with a conditional generator expression

I was running a piece of code that unexpectedly gave a logic error at one part of the program. When investigating the section, I created a test file to test the set of statements being run and found out an unusual bug that seems very odd. I tested…
45
votes
4 answers

Are list comprehensions syntactic sugar for `list(generator expression)` in Python 3?

In Python 3, is a list comprehension simply syntactic sugar for a generator expression fed into the list function? e.g. is the following code: squares = [x**2 for x in range(1000)] actually converted in the background into the following? squares =…
37
votes
3 answers

List comprehension vs generator expression's weird timeit results?

I was answering this question, I preferred generator expression here and used this, which I thought would be faster as generator doesn't need to create the whole list first: >>> lis=[['a','b','c'],['d','e','f']] >>> 'd' in (y for x in lis for y in…
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
26
votes
2 answers

next() doesn't play nice with any/all in python

I ran down a bug today that came about because I was using next() to extract a value, and 'not found' emits a StopIteration. Normally that would halt the program, but the function using next was being called inside an all() iteration, so the all…
amwinter
  • 3,121
  • 2
  • 27
  • 25
23
votes
2 answers

Python generator expression parentheses oddity

I want to determine if a list contains a certain string, so I use a generator expression, like so: g = (s for s in myList if s == myString) any(g) Of course I want to inline this, so I do: any((s for s in myList if s == myString)) Then I think it…
Ari
  • 3,460
  • 3
  • 24
  • 31
21
votes
2 answers

Using while in list comprehension or generator expressions

I can use if and for in list comprehensions/generator expressions as list(i for i in range(100) if i*i < 30) I know this is not the most efficient but bear with me as the condition could be much more complicated and this is just an example.…
highBandWidth
  • 16,751
  • 20
  • 84
  • 131
20
votes
6 answers

Unexpected output from list(generator)

I have a list and a lambda function defined as In [1]: i = lambda x: a[x] In [2]: alist = [(1, 2), (3, 4)] Then I try two different methods to calculate a simple sum First method. In [3]: [i(0) + i(1) for a in alist] Out[3]: [3, 7] Second…
16
votes
3 answers

Differences between generator comprehension expressions

There are, as far as I know, three ways to create a generator through a comprehension1. The classical one: def f1(): g = (i for i in range(10)) The yield variant: def f2(): g = [(yield i) for i in range(10)] The yield from variant (that…
Right leg
  • 16,080
  • 7
  • 48
  • 81
16
votes
5 answers

Generator expressions Python

I have a list of dictionaries like the following: lst = [{'a': 5}, {'b': 6}, {'c': 7}, {'d': 8}] I wrote a generator expression like: next((itm for itm in lst if itm['a']==5)) Now the strange part is that though this works for the key value pair…
15
votes
7 answers

Use case for nested/multiple list comprehensions or generator expressions. When is it more elegant?

I see this kind of thing sometimes: (k for k in (j for j in (i for i in xrange(10)))) Now this really bends my brain, and I would rather it wasn't presented in this way. Are there any use-cases, or examples of having used these nested expressions…
Ali Afshar
  • 40,967
  • 12
  • 95
  • 109
15
votes
8 answers

convert string to dict using list comprehension

I have came across this problem a few times and can't seem to figure out a simple solution. Say I have a string string = "a=0 b=1 c=3" I want to convert that into a dictionary with a, b and c being the key and 0, 1, and 3 being their respective…
Pavel
  • 729
  • 2
  • 11
  • 22
1
2 3
9 10