Skip to content

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 o…

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 …

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 iterati…

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 …