From the course: Python: Programming Efficiently
Unlock the full course today
Join today to access over 23,300 courses taught by industry experts.
Comprehensions and generators - Python Tutorial
From the course: Python: Programming Efficiently
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.…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
(Locked)
Writing Python efficiently50s
-
(Locked)
Python vs. C4m 40s
-
(Locked)
Design efficient loops7m 3s
-
(Locked)
Comprehensions and generators6m 41s
-
(Locked)
Exploit Python collections7m 14s
-
(Locked)
Write Pythonic code6m 44s
-
(Locked)
Challenge: Analyze Olympic medalist data1m 20s
-
(Locked)
Solution: Analyze Olympic medalist data1m 33s
-
(Locked)
-
-
-
-