close

[Solved] “‘generator’ object is not subscriptable” error

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error “‘generator’ object is not subscriptable” error in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How “‘generator’ object is not subscriptable” Error Occurs?

Today I get the following error “‘generator’ object is not subscriptable” error in python.

How To Solve “‘generator’ object is not subscriptable” Error ?

  1. How To Solve “'generator' object is not subscriptable” Error ?

    To Solve “'generator' object is not subscriptable” Error For such algorithms, you can apply kind of a “sliding window” technique, demonstrated below in a stripped-down version of your code:

  2. “'generator' object is not subscriptable” error

    To Solve “'generator' object is not subscriptable” Error For such algorithms, you can apply kind of a “sliding window” technique, demonstrated below in a stripped-down version of your code:

Solution 1

As an extension to Jeremy’s answer some thoughts about the design of your code:

Looking at your algorithm, it appears that you do not actually need truly random access to the values produced by the generator: At any point in time you only need to keep four consecutive values (three, with an extra bit of optimization). This is a bit obscured in your code because you mix indexing and iteration: If indexing would work (which it doesn’t), your y could be written as x[p + 0].

For such algorithms, you can apply kind of a “sliding window” technique, demonstrated below in a stripped-down version of your code:

import itertools, functools, operator
vs = [int(v) for v in itertools.islice(x, 3)]
for v in x:
    vs.append(int(v))
    currentProduct = functools.reduce(operator.mul, vs, 1)
    print(currentProduct)
    vs = vs[1:]

Solution 2

Your x value is is a generator object, which is an Iterator: it generates values in order, as they are requested by a for loop or by calling next(x).

You are trying to access it as though it were a list or other Sequence type, which let you access arbitrary elements by index as x[p + 1].

If you want to look up values from your generator’s output by index, you may want to convert it to a list:

x = list(x)

This solves your problem, and is suitable in most cases. However, this requires generating and saving all of the values at once, so it can fail if you’re dealing with an extremely long or infinite list of values, or the values are extremely large.

If you just needed a single value from the generator, you could instead use itertools.islice(x, p) to discard the first p values, then next(...) to take the one you need. This eliminate the need to hold multiple items in memory or compute values beyond the one you’re looking for.

import itertools

result = next(itertools.islice(x, p))

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read