🌐
W3Schools
w3schools.com › python › ref_func_next.asp
Python next() Function
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training · ❮ Built-in Functions · Create an iterator, and print the items one by one: mylist = iter(["apple", "banana", "cherry"]) x = next(mylist) print(x) x = next(mylist) print(x) x = next(mylist) print(x) Try it Yourself » ·
🌐
Python documentation
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.3 documentation
February 27, 2026 - Retrieve the next item from the iterator by calling its __next__() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised. ... This is the ultimate base class of all other classes.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-next-method
Python next() method - GeeksforGeeks
December 19, 2025 - The next() function returns the next item from an iterator. If there are no more items, it raises a StopIteration error, unless you provide a default value.
🌐
Python
peps.python.org › pep-3114
PEP 3114 – Renaming iterator.next() to iterator.__next__()
March 4, 2007 - The iterator protocol in Python 2.x consists of two methods: __iter__() called on an iterable object to yield an iterator, and next() called on an iterator object to yield the next item in the sequence.
🌐
Code-maven
python.code-maven.com › python-functional-programming › using-iterator-with-next.html
Using iterator with next - Functional programming in Python
from counter import Counter cnt = Counter() while True: try: a = next(cnt) print(a) except Exception as ex: print(ex.__class__.__name__) break
🌐
Python Morsels
pythonmorsels.com › next
Python's next() function - Python Morsels
July 17, 2023 - That function manually calls next on iterators corresponding to each of the given iterables, one after the other. If you're curious about that sentinel = object() line, see Unique sentinel values in Python.
Find elsewhere
🌐
Real Python
realpython.com › ref › builtin-functions › next
next() | Python’s Built-in Functions – Real Python
The .__next__() method processes the current number and returns its square value. When there are no more items, .__next__() raises the StopIteration exception. ... In this tutorial, you'll learn what iterators and iterables are in Python.
🌐
iO Flood
ioflood.com › blog › python-next
Python next() Function Guide (With Examples)
June 12, 2024 - Using the next() function in Python programming is crucial while automating tasks at IOFLOOD, as it allows sequential access to elements in an iterator. In our experience, the next() function streamlines data traversal and facilitates efficient ...
🌐
Programiz
programiz.com › python-programming › methods › built-in › next
Python next()
The next() function returns the next item from the iterator. In this tutorial, we will learn about the Python next() function in detail with the help of examples.
🌐
LabEx
labex.io › home › builtin › next
Python next() built-in function - Python Cheatsheet
From the Python 3 documentation ... the iterator is exhausted, otherwise StopIteration is raised. The next() function retrieves the next item from an iterator....
🌐
Pynerds
pynerds.com › python-next-function
Python next() Function
The next() function is used to retrieve the next item from an iterator object. If the iterator is empty, the function raises a StopIteration exception. In Python, an iterator is an object that implements the iterator protocol, which consists of the __iter__() and __next__() methods.
🌐
Tutorialspoint
tutorialspoint.com › python › python_next_function.htm
Python next() Function
The Python next() function is used to iterate through the contents of a Python iterator object, an object representing a stream of data. If you pass an iterator object to this function, it returns the next item in it.
🌐
Pierian Training
pieriantraining.com › home › understanding the ‘next’ keyword in python
Understanding the 'next' Keyword in Python - Pierian Training
April 28, 2023 - When we call the `next()` function on an iterator object, it returns the next value from the sequence. If there are no more elements, it raises the `StopIteration` exception. It is important to note that once an iterator has reached its end, ...
Top answer
1 of 2
79

The expression (sum(row) for row in M) creates what's called a generator. This generator will evaluate the expression (sum(row)) once for each row in M. However, the generator doesn't do anything yet, we've just set it up.

The statement next(G) actually runs the generator on M. So, if you run next(G) once, you'll get the sum of the first row. If you run it again, you'll get the sum of the second row, and so on.

>>> M = [[1,2,3],
...      [4,5,6],
...      [7,8,9]]
>>> 
>>> G = (sum(row) for row in M) # create a generator of row sums
>>> next(G) # Run the iteration protocol
6
>>> next(G)
15
>>> next(G)
24

See also:

  • Documentation on generators
  • Documentation on yield expressions (with some info about generators)
2 of 2
10

If you've come that far, then you should already know how a common for-in statement works.

The following statement:

for row in M: print row

would see M as a sequence of 3 rows (sub sequences) consisting of 3 items each, and iterate through M, outputting each row on the matrix:

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

You knew that, well...

You can see Generators just as some syntactic sugar around for-in loops. Forget about the sum() call, and type something like this on IDLE:

G = (row for row in M)
print G
for a in G: print a

You see, the Generator cannot be directly represented as text, not just as a sequence can be. But, you can iterate through a Generator as if it were a sequence.

You'll find some big differences then, but the basics are that you can use a generator not to return just the value of each item in the sequence, but the result of any expression. In the tutorial's example, the expression is sum(row).

Try the following and see what happens:

G = ("("+str(row[2])+";"+str(row[1])+";"+str(row[0])+")" for row in M)
G.next()
G.next()
G.next()
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › file › next.html
next — Python Reference (The Right Way) 0.1 documentation
>>> f = open(r'C:\aiw.txt') >>> f.next() " ALICE'S ADVENTURES IN WONDERLAND\n" >>> f.next() '\n' >>> f.next() ' Lewis Carroll\n'
🌐
EITCA
eitca.org › home › how can you use the `next()` function to retrieve the next element in an iterator?
How can you use the `next()` function to retrieve the next element in an iterator? - EITCA Academy
August 3, 2023 - The `next()` function in Python is used to retrieve the next element from an iterator. An iterator is an object that implements the iterator protocol, which consists of two methods: `__iter__()` and `__next__()`. To understand how the `next()` function works, let's first discuss iterators and ...
🌐
Codecademy
codecademy.com › docs › python › built-in functions › next()
Python | Built-in Functions | next() | Codecademy
July 8, 2025 - In Python, the next() function returns the next element from an iterator object.
🌐
Interactive Chaos
interactivechaos.com › en › python › function › next
next | Interactive Chaos
January 14, 2021 - Python scenarios · Full name · next · Library · Built-in · Syntax · next(iterator [, default]) Description · The next function returns the next element of the iterator included as the first argument by invoking its __next__ method.