From the course: Python: Programming Efficiently

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Comprehensions and generators

Comprehensions and generators - Python Tutorial

From the course: Python: Programming Efficiently

Start my 1-month free trial

Comprehensions and generators

- [Instructor] We move on from loops and iterators to two powerful related constructs, comprehensions and generators. Comprehension are a handy way to run a loop within a single line of code, and to collect the results of the loop in a collection, such as a list. Generators are a shortcut to write functions that implement iterators. Both are used extensively in Pythonic code. We load all the packages we need. Here is some code from the last video to load the Olympic cities. I have saved the geolocation coordinates in a JSON file. The standard library model, JSON, loads it as a dictionary. The Python designers gave us a very convenient syntax to use in the very common situation where we loop over a sequence, possibly we select a subset of elements. We operate on each element individually and we collect the resulting elements in a collection. For instance, say we want a list of all the Olympic games after World War two.…

Contents