0

Why indexing is not possible in Generators ?

27th Aug 2022, 5:34 PM
Levi
Levi - avatar
3 odpowiedzi
0
generator is reference id. You convert it into a iterable by using for loop.. . If you show your troubling code then you may get solution if you need...
29th Aug 2022, 9:14 AM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 where is the generator stored in memory ?_
29th Aug 2022, 4:56 PM
Levi
Levi - avatar
0
Generators not stored in memory. They just hold variables which are using yielding or returning value. It can accessed only once. See example: def func() : i = 0 while i < 10: yield i i += 1 gen = func() # gen hold object of generator. You can access value by calling next(gen) 1 at a time. After all values access, the generated object is discarded. If you need again, you need to generate object again. By for loop, you can do like : for i in func() : print(i) Generators are lazy evaluations. Only return values when you call it, unlike list iterators which stores entire data on once call. So you can access it by indexing. But in generator, next value is available only if you consume current value. So you only access one at a time. Hope it helps...
29th Aug 2022, 8:00 PM
Jayakrishna 🇮🇳