generator-expression

List extend with a generator expression referencing the list itself

List extend with a generator expression referencing the list itself Question: Consider the code below: a = [1, 2] a.extend(x*2 for x in a) print(a) At first glance, it would seem (to the uninitiated) that the code will generate the following output: [1, 2, 2, 4] But it does NOT generate that output. In fact, …

Total answers: 2

What is the difference between map objects and generator objects?

What is the difference between map objects and generator objects? Question: Which of these is best to use in which situations? I’m referring to object made with map(), generator expressions, and generator functions. Here’s code I used to view these objects. str_ints = (str(x)for x in range(3)) f = len map_obj = map(f, str_ints) gen_obj_from_gen_expression …

Total answers: 1

Unexpected behaviour with a conditional generator expression

Unexpected behaviour with a conditional generator expression Question: 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 …

Total answers: 8

How can this generator function be turned into a generator expression?

How can this generator function be turned into a generator expression? Question: I have written a generator function that yields an infinite sequence of strings in the same fashion as the column-naming scheme in a spreadsheet application like Excel, e.g.: ”, ‘A’, ‘B’, … ‘Z’, ‘AA’, ‘AB’, … ‘AZ’, ‘BA’, … ‘ZZ’, ‘AAA’, … My …

Total answers: 1

Generator as function argument

Generator as function argument Question: 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 not work, as expected. >>> f(*[2], 1) File "<stdin>", …

Total answers: 1

yield in list comprehensions and generator expressions

yield in list comprehensions and generator expressions Question: The following behaviour seems rather counterintuitive to me (Python 3.4): >>> [(yield i) for i in range(3)] <generator object <listcomp> at 0x0245C148> >>> list([(yield i) for i in range(3)]) [0, 1, 2] >>> list((yield i) for i in range(3)) [0, None, 1, None, 2, None] The intermediate …

Total answers: 1

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

Are list comprehensions syntactic sugar for `list(generator expression)` in Python 3? Question: 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 = list(x**2 for …

Total answers: 4

List comprehension with condition

List comprehension with condition Question: Suppose I have a list a = [0, 1, 2]. I know that I can use a list comprehension like so, to get a list where the values are doubled: >>> b = [x*2 for x in a] >>> b [0, 2, 4] How can I make it so that …

Total answers: 2

List comprehension vs generator expression's weird timeit results?

List comprehension vs generator expression's weird timeit results? Question: 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 x) True And Levon …

Total answers: 3