June 2017
Beginner to intermediate
274 pages
6h 49m
English
Python has a special kind of expression called a comprehension. Comprehensions are variations of the special syntax for creating dictionaries, lists, and sets.
Let's look at some examples. Here we see a list comprehension:
capitals = [x.upper() for x in example_list]
What this expression does is that it creates a new list containing the uppercase versions of the words in the old list.
The first part after the opening square bracket is an x.upper() expression. This expression describes how to derive a member of the new list from a member of the old list. After that is the for keyword, then the name of the x variable we used in the first expression to represent the values from the old list. Then, the keyword is followed by the ...
Read now
Unlock full access