Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: next() function

Tags:

python

sum

next

I'm learning Python from a book, and I came across this example:

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 

Since I'm an absolute beginner, and the author hasn't provided any explanation of the example or the next() function, I don't understand what the code is doing.

like image 286
eozzy Avatar asked Nov 14 '09 02:11

eozzy


People also ask

What is next () function in Python?

Python next() Function The next() function returns the next item in an iterator. You can add a default return value, to return if the iterable has reached to its end.

What is __ next __ in Python?

The __next__() method must return the next item in the sequence. On reaching the end, and in subsequent calls, it must raise StopIteration . Here, we show an example that will give us the next power of 2 in each iteration.

What is list next in Python?

Python next() method returns the next element from the list; if not present, prints the default value. If the default value is not present, raise the StopIteration error. You can add a default return value to return if the iterable has reached its end.


2 Answers

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)
like image 87
jtbandes Avatar answered Oct 08 '22 14:10

jtbandes


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() 
like image 21
Simón Avatar answered Oct 08 '22 12:10

Simón