337

I have a generator function like the following:

def myfunct():
  ...
  yield result

The usual way to call this function would be:

for r in myfunct():
  dostuff(r)

My question, is there a way to get just one element from the generator whenever I like? For example, I'd like to do something like:

while True:
  ...
  if something:
      my_element = pick_just_one_element(myfunct())
      dostuff(my_element)
  ...
codeforester
  • 39,467
  • 16
  • 112
  • 140
Alexandros
  • 4,425
  • 4
  • 23
  • 21

8 Answers8

450

Create a generator using

g = myfunct()

Everytime you would like an item, use

next(g)

(or g.next() in Python 2.5 or below).

If the generator exits, it will raise StopIteration. You can either catch this exception if necessary, or use the default argument to next():

next(g, default_value)
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • 7
    Note, it will only raise StopIteration when you try to use g.next() after the last item in g has been provided. – Wilduck Jan 19 '11 at 22:11
  • 34
    `next(gen, default)` may also be used to avoid the `StopIteration` exception. For example `next(g, None)` for a generator of strings will either yield a string or None after the iteration was finished. – Attila Mar 06 '13 at 14:18
  • 9
    in Python 3000, next() is __next__() – Jonathan Baldwin Apr 10 '14 at 02:52
  • 31
    @JonathanBaldwin: Your comment is somewhat misleading. In Python 3, you would use the second syntax given in my answer, `next(g)`. This will internally call `g.__next__()`, but you don't really have to worry about that, just as you usually don't care that `len(a)` internally calls `a.__len__()`. – Sven Marnach Apr 10 '14 at 10:31
  • 14
    I should have been more clear. `g.next()` is `g.__next__()` in py3k. The builtin `next(iterator)` has been around since Python 2.6, and is what should be used in all new Python code, and it's trivial to backimplement if you need to support py <= 2.5. – Jonathan Baldwin Apr 10 '14 at 19:58
  • I think [@kxr](http://stackoverflow.com/users/1184933) provides quite good reasons (five years later, hence the lack of upvotes) for not explicitly bothering with `next()` and the `StopIteration` exception in [their excellent answer](http://stackoverflow.com/a/35370041/260122). `itertools.islice()` is a real gem, nice and Pythonic, and one should not be afraid of the for loop in favor of one-liners. – clacke Aug 05 '16 at 14:50
  • @clacke: These "good reasons" are that it is "odd", "rarely used", "very low-level practice" and "non-pythonic". It's easy to prove that "rarely used" is wrong. The other "reasons" are simply stated without any reference or rationale, and I can't tell how kxr came to these conclusions. In my opinion, the built-in `next()` is provided for exactly this purpose, so it's the natural function to use, and it is what most people use for this purpose. – Sven Marnach Aug 06 '16 at 11:14
  • @SvenMarnach The statement is that "Using next() *should* be very rare" (my emphasis). It's a matter of interpretation, but I think the intended meaning is not an observation on actual use, but on best practice. What's more pythonic is a matter of taste, of course. For my part, I agree with [@kxr](http://stackoverflow.com/a/35370041/260122) that using the higher-level protocol makes for less obscure code, even though `next()` isn't *that* obscure. I'm grateful for the tip about the existance of `islice()`, which was exactly what I was looking for when I found this question. – clacke Aug 10 '16 at 09:56
  • I've found that for picking one item from a generator the `next(g, None)` syntax combines nicely with the walrus operator to proceed if the generator yields and item: `if (item := next(g, None)): print(item)`. This also combines nicely with `filter`, i.e. `next(filter(cond, iterable), None)` – jlhasson May 18 '22 at 21:04
34

For picking just one element of a generator use break in a for statement, or list(itertools.islice(gen, 1))

According to your example (literally) you can do something like:

while True:
  ...
  if something:
      for my_element in myfunct():
          dostuff(my_element)
          break
      else:
          do_generator_empty()

If you want "get just one element from the [once generated] generator whenever I like" (I suppose 50% thats the original intention, and the most common intention) then:

gen = myfunct()
while True:
  ...
  if something:
      for my_element in gen:
          dostuff(my_element)
          break
      else:
          do_generator_empty()

This way explicit use of generator.next() can be avoided, and end-of-input handling doesn't require (cryptic) StopIteration exception handling or extra default value comparisons.

The else: of for statement section is only needed if you want do something special in case of end-of-generator.

Note on next() / .next():

In Python3 the .next() method was renamed to .__next__() for good reason: its considered low-level (PEP 3114). Before Python 2.6 the builtin function next() did not exist. And it was even discussed to move next() to the operator module (which would have been wise), because of its rare need and questionable inflation of builtin names.

Using next() without default is still very low-level practice - throwing the cryptic StopIteration like a bolt out of the blue in normal application code openly. And using next() with default sentinel - which best should be the only option for a next() directly in builtins - is limited and often gives reason to odd non-pythonic logic/readablity.

Bottom line: Using next() should be very rare - like using functions of operator module. Using for x in iterator , islice, list(iterator) and other functions accepting an iterator seamlessly is the natural way of using iterators on application level - and quite always possible. next() is low-level, an extra concept, unobvious - as the question of this thread shows. While e.g. using break in for is conventional.

KJH
  • 2,382
  • 16
  • 26
kxr
  • 4,841
  • 1
  • 49
  • 32
  • 15
    This is way too much work for just getting the first element of a list result. Often enough I do not need it to be lazy but do not have a choice in py3. Is there not something akin to `mySeq.head` ? – WestCoastProjects Mar 03 '18 at 15:57
  • 1
    in my opinion, the `for … break` pattern is bad because the intention of `for` is unclear until encountering the `break` at the end (and the problem here lies with that it must be placed at the end), so at least one should write a comment at the `for`; I would prefer to see a `with x in :` syntax – ToxiCore Nov 23 '22 at 14:48
  • 1
    According to [PEP 3114](https://peps.python.org/pep-3114/), `next` was renamed for method definitions but the built-in method wasn't changed. In other words, `next(x)` is fine in Python 3, but `x.next()` won't work. – Stevoisiak Jan 13 '23 at 19:35
18

Generator is a function that produces an iterator. Therefore, once you have iterator instance, use next() to fetch the next item from the iterator. As an example, use next() function to fetch the first item, and later use for in to process remaining items:

# create new instance of iterator by calling a generator function
items = generator_function()

# fetch and print first item
first = next(items)
print('first item:', first)

# process remaining items:
for item in items:
    print('next item:', item)
andrii
  • 1,248
  • 1
  • 11
  • 25
9

You can pick specific items using destructuring, e.g.:

>>> first, *middle, last = range(10)
>>> first
0
>>> middle
[1, 2, 3, 4, 5, 6, 7, 8]
>>> last
9

Note that this is going to consume your generator, so while highly readable, it is less efficient than something like next(), and ruinous on infinite generators:

>>> first, *rest = itertools.count()

Mark McDonald
  • 7,571
  • 6
  • 46
  • 53
3

I don't believe there's a convenient way to retrieve an arbitrary value from a generator. The generator will provide a next() method to traverse itself, but the full sequence is not produced immediately to save memory. That's the functional difference between a generator and a list.

g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
0
generator = myfunct()
while True:
   my_element = generator.next()

make sure to catch the exception thrown after the last element is taken

John
  • 5,561
  • 1
  • 23
  • 39
  • Not valid for Python 3, see the excellent [answer by kxr](http://stackoverflow.com/a/35370041/260122). – clacke Aug 05 '16 at 14:46
  • 2
    Just replace "generator.next()" with "next(generator)" for Python 3. – Roy Feb 04 '19 at 17:50
0

For those of you scanning through these answers for a complete working example for Python3... well here ya go:

def numgen():
    x = 1000
    while True:
        x += 1
        yield x

nums = numgen() # because it must be the _same_ generator

for n in range(3):
    numnext = next(nums)
    print(numnext)

This outputs:

1001
1002
1003
Dave Rove
  • 913
  • 1
  • 12
  • 18
-3

I believe the only way is to get a list from the iterator then get the element you want from that list.

l = list(myfunct())
l[4]
keegan3d
  • 10,357
  • 9
  • 53
  • 77
  • Sven's answer is probably better, but I'll just leave this here incase it's more inline with your needs. – keegan3d Jan 19 '11 at 22:00
  • 28
    Make sure that you have a finite generator before doing this. – Seth Jan 19 '11 at 22:21
  • 7
    Sorry, this has complexity the length of the iterator, while the problem is obviously O(1). – yo' Oct 21 '14 at 16:30
  • 1
    Wasting too many memory and process to suck from generator! Also, as @Seth mentioned before, generators are not guaranteed for, when to stop generating. – pylover Sep 16 '16 at 18:41
  • This is obviously _not_ the only way (or the best way if `myfunct()` generates a large number of values) since you can use the built-in function `next` to get the next generated value. – HelloGoodbye Nov 20 '17 at 21:15