Loop better: a deeper look at iteration in Python

North Bay Python · 6K views · 2017 · 27:39

Open on YouTube

Python, Ruby & scripting languagesProgramming language designtalk

Why Stack Overflow recommends it

Stack Overflow users link it in answers about iterators, generators, and the iteration protocol as a clear explanation of how Python loops work under the hood.

Recommended by 2 people across 2 different months, 2018 to 2020.

What it is

A 27-minute conference talk by Trey Hunner at North Bay Python 2017 on how looping works in Python. It explains sequences, iterables, iterators, the iterator protocol behind for loops, generators, generator expressions, and infinite iterables.

What people said

Recommends Piotr Banaś · 2018-03 · Stack Overflow · on “Flattening list of list using other technique”

Iterate protocol In python there is such a key concept as iteration protocol. Iteration protocol is used every time you: Use a for loop Unpack/pack a tuple Use list comprehension Yield any value Basically iteration protocol is used every time you want to process some sequence of data. Let's use list creation as example. Whenever you call: my_list=list(x) Python interpreter is calling __next__ [LINK] method on x and then adding result to end of result list (which is empty at the beginning). And it stops when next(x) throws StopIteration [LINK] . So what with this yields? Yield [LINK] is pretty similar to return but it does not terminate the function. Result of the function with yields inside

Recommends Niloct · 2020-07 · Stack Overflow · on “Django: Subprocess Continuous Output To HTML View”

You could simplify your task using StreamingHttpResponse [LINK] and Popen [LINK] : def test_iterator(): from subprocess import Popen, PIPE, CalledProcessError with Popen(['ping', 'localhost'], stdout=PIPE, bufsize=1, universal_newlines=True) as p: for line in p.stdout: yield(line + '<br>') # process line here if p.returncode != 0: raise CalledProcessError(p.returncode, p.args) def busy_view(request): from django.http import StreamingHttpResponse return StreamingHttpResponse(test_iterator()) StreamingHttpResponse expects an iterator as its parameter. An iterator function is one which has a yield expression (or a generator expression), and its return value is a generator object (an iterator).

Cites Jalbrecq · 2020-08 · Stack Overflow · on “what is the use of iterator in python ? already we have looping”

Short Answer Here is an example of how you can improve your code with iterators: // WITHOUT ITERATORS hours_worked = 0 for event in events: if event.is_billable(): hours_worked += event.duration // WITH ITERATORS // create an iterators with the duration of the billable event. billable_times = (event.duration for event in events if event.is_billable()) hours_worked = sum(billable_times) With the iterators we have named something that was unnamed before and we can now use the sum function. Naming things make our code more descriptive and more readable. Learn more here: Python Basics: Iteration, Iterables, Iterators, and Looping [LINK] Long Answer Python doesn't have the same for loops as other