97

Is there a more syntactically concise way of writing the following?

gen = (i for i in xrange(10))
index = 5
for i, v in enumerate(gen):
    if i is index:
        return v

It seems almost natural that a generator should have a gen[index] expression, that acts as a list, but is functionally identical to the above code.

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
Oliver Zheng
  • 7,831
  • 8
  • 53
  • 59
  • 15
    You don't want `is` in this situation (or many situations at all). `is` is for comparing identity, not equality. You want `==`. This will probably work in this instance, but only by coincidence and implementation detail. – Mike Graham Feb 20 '10 at 06:15
  • 1
    Since I'm using integers, how could it not work? Is it even good practice to expect the `index` object to implement `__eq__` in cases such as this? (This is getting off topic...) – Oliver Zheng Feb 20 '10 at 08:34
  • 2
    Try `1000 is 500 + 500`, it will (probably) be `False`. See, for example, http://stackoverflow.com/questions/306313/python-is-operator-behaves-unexpectedly-with-integers – Scott Griffiths Feb 20 '10 at 08:49
  • 2
    +1 for this question. It does seem strange that there's not a less verbose way to say "the nth result of gen". – LarsH Oct 18 '13 at 01:39
  • Another possibility is zippers --- they handle arbitrary trees, but a list is a tree too. See this implementation https://github.com/trivio/zipper/blob/master/tests/test_zipper.py – Reb.Cabin Jul 06 '17 at 20:52

10 Answers10

100

one method would be to use itertools.islice

>>> gen = (x for x in range(10))
>>> index = 5
>>> next(itertools.islice(gen, index, None))
5
hwjp
  • 15,359
  • 7
  • 71
  • 70
cobbal
  • 69,903
  • 20
  • 143
  • 156
19

I think the best way is :

next(x for i,x in enumerate(it) if i==n)

(where it is your iterator and n is the index)

It doesn't require you to add an import (like the solutions using itertools) nor to load all the elements of the iterator in memory at once (like the solutions using list).

Note 1: this version throws a StopIteration error if your iterator has less than n items. If you want to get None instead, you can use :

next((x for i,x in enumerate(it) if i==n), None)

Note 2: There are no brackets inside the call to next. This is not a list comprehension, but a generator comprehension, that does not consume the original iterator further than its nth element.

lovasoa
  • 6,419
  • 1
  • 35
  • 45
17

You could do this, using count as an example generator:

from itertools import islice, count
next(islice(count(), n, n+1))
Frozen Flame
  • 3,135
  • 2
  • 23
  • 35
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
11

I'd argue against the temptation to treat generators like lists. The simple but naive approach is the simple one-liner:

gen = (i for i in range(10))
list(gen)[3]

But remember, generators aren't like lists. They don't store their intermediate results anywhere, so you can't go backwards. I'll demonstrate the problem with a simple example in the python repl:

>>> gen = (i for i in range(10))
>>> list(gen)[3]
3
>>> list(gen)[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

Once you start going through a generator to get the nth value in the sequence, the generator is now in a different state, and attempting to get the nth value again will return you a different result, which is likely to result in a bug in your code.

Let's take a look at another example, based on the code from the question.

One would initially expect the following to print 4 twice.

gen = (i for i in range(10))
index = 4
for i, v in enumerate(gen):
    if i == index:
        answer = v
        break
print(answer)
for i, v in enumerate(gen):
    if i == index:
        answer = v
        break
print(answer)

but type this into the repl and you get:

>>> gen = (i for i in range(10))
>>> index = 4
>>> for i, v in enumerate(gen):
...     if i == index:
...             answer = v
...             break
... 
>>> print(answer)
4
>>> for i, v in enumerate(gen):
...     if i == index:
...             answer = v
...             break
... 
>>> print(answer)
9

Good luck tracing that bug down.


As pointed out, if the generator is infinitely long, you can't even convert it to a list. The expression list(gen) will never finish.

There is a way you could put a lazily evaluated caching wrapper around an infinite generator to make it look like an infinitely long list you could index into at will, but that deserves its own question and answer, and would have major performance implications.

Neuron
  • 5,141
  • 5
  • 38
  • 59
2

If n is known at authoring-time, you can use destructuring. e.g. to get the 3rd item:

>>> _, _, third, *rest = range(10)
>>> third
2
>>> rest
[3, 4, 5, 6, 7, 8, 9]
Mark McDonald
  • 7,571
  • 6
  • 46
  • 53
2

My solution would be:

[_ for _ in range(n-1) if next(gen) and False ]
return next(gen) 

As next(gen) and False is always false, the list comprehension does nothing but executing next(gen) n-1 times.

In my testing, it is as fast as using itertools.islice

Madlozoz
  • 357
  • 4
  • 15
  • 1
    I like it a lot, this doesn't import `itertools` or pretends to create a "slice", instead simply "jumps" through the iterator and returns the `next()` value. – Nikolaj Š. Aug 19 '22 at 12:16
  • Check out [my take on this](https://stackoverflow.com/a/73425727/9288580), with some criticisms =) – Nikolaj Š. Aug 20 '22 at 10:00
  • 1
    @KlasŠ. you misread my solution. As "next(gen) and False" is always false, it never adds anything to the list. It only creates an empty list, make some kind of empty loop then discard the still empty list. It means to be memory efficient. – Madlozoz Aug 21 '22 at 18:12
  • Right you are, totally missed that, my bad =) – Nikolaj Š. Aug 22 '22 at 10:44
0

The first thing that came to my mind was:

gen = (i for i in xrange(10))
index = 5

for i, v in zip(range(index), gen): pass

return v
Alexey
  • 3,843
  • 6
  • 30
  • 44
0

Building up on @Madlozoz answer, but with mighty walrus operator:

>>> gen = (x ** 2 for x in itertools.count())
>>> [v := next(gen) for _ in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> v
81

The thing I don't like about this (unlike Madlozoz') solution is that potentially huge list is constructed just to be discarded immediately.


A simple for loop is another way:

# `gen` continues from the previous snippet
>>> for _ in range(3):
...     v = next(gen)
...
>>> print(v)
144

At a cost of extra line we can save some process ticks on assignments as well and spend them on wrapper class:

class IterIndexer:

    def __init__(self, iter_):
        self.iter = iter_

    def __getitem__(self, i):
        for _ in range(i - 1):
            next(self.iter)
        return next(self.iter)


gen = (x ** 2 for x in itertools.count())
gen = IterIndexer(gen)
print(gen[14])
169

It'd be even cooler to wrap it properly, so that you can use wrapper instance for everything instead of original generator or iterator, but that's another question =)

Nikolaj Š.
  • 1,457
  • 1
  • 10
  • 17
-2

Perhaps you should elaborate more on a actual use case.

>>> gen = xrange(10)
>>> ind=5 
>>> gen[ind]
5
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • 4
    I editted `xrange(10)` to `(i for i in xrange(10))`. Turns out this syntax works for `xrange` since it's not really a generator... – Oliver Zheng Feb 20 '10 at 02:21
  • 5
    `xrange` predates generators, and returns an xrange object, which actually implements the full sequence protocol. – Mike Graham Feb 20 '10 at 06:16
-2

Best to use is : example :

a = gen values ('a','c','d','e')

so the answer will be :

a = list(a) -> this will convert the generator to a list (it will store in memory)

then when you want to go specific index you will :

a[INDEX] -> and you will able to get the value its holds 

if you want to know only the count or to do operations that not required store in memory best practice will be : a = sum(1 in i in a) -> this will count the number of objects you have

hope i made it more simple.

Ido Bleicher
  • 709
  • 1
  • 9
  • 19