Skip to content
Advertisement

Tag: generator

Why list([]) weighs less than []

I have such code: With the next result: The question is: Why does a list created with list([]) weigh less than a list created with just [] or for _ in condition? Answer (The details in this answer depend on the implementation, they are written to match CPython 3.10.0. Other versions or other implementations of Python work differently.) Lists in

Can you yield from a lambda function?

I have a generator function in a class: In another function I initalize it as a variable: And it’s yielded as necessary: Can defining the generator be done in one line, however? I’ve considered the below: Here’s a minimal code I’m working with: Output is below: I just wanted to see if I could get rid of Foo.generator and instead

Iterables / generators with specified length

Iterable objects are those that implement __iter__ function, which returns an iterator object, i.e. and object providing the functions __iter__ and __next__ and behaving correctly. Usually the size of the iterable object is not known beforehand, and iterable object is not expected to know how long the iteration will last; however, there are some cases in which knowing the length

python – different behavior of print(*generator, )

Question Please help understand why the two cases act differently although both use a generator (i for i in range(5)). Answer When you use the * operator on a generator expression (or any iterator for that matter), it consumes it: You’d need to recreate it to re-use it, which is what you’re doing in your first example: Note on range

What type of iterable is “x for x in y”?

I’ve been confused of list comprehension in Python, although its shorthand doing for loop is very convenient. I’m not sure if what’s in the join() function’s parameter below is list comprehension since usually you put [] around to show it’s a list comprehension. My question: What’s the type of iterable produced by str(x) for x in res[::-1] in join() below?

Advertisement