Comprehensions
Comprehensions in Python provide a short and clear way to create new sequences from existing iterables. Basicly they are a fancy syntax for simple for-loop pattern.
Lesson Overviewā
At the end of the lesson you will know:
- What are comprehensions in Python
- How to write
list,dictorsetcoprehension - How to use conditions in coprehensions
The key to understanding list comprehensions is that theyāre just for-loops over a collection expressed in a more terse and compact syntax.
We will start with list coprehension as it is most common.
List Coprehensionā
Syntax for this looks like:
[item for item in iterable]
Let's imagine we have a task of creating a list of square numbers from some other list of numbers. For example, let's assume we have a following list of numbers:
nmb_list = [1, 2, 3, 4, 5, 6, 7, 8]
To create a new list with square of all numbers in a list, we need to first get each element in the list and apply a mathematical operation on it. So it would look something like this:
And the result is correct, but in Python, we can do better. Let's convert our for-loop to list coprehension.
Result is completly the same, but our code is simpler and more concise. In this simple example we may not see the benefit, but let's add a check there, to only collect even numbers.
If we use for-loop we may do something like this.
If we use list comprehension it would look like this:
The result is again, completly the same, but the syntax is shorter and more concise.
You do not need to worry about understanding coprehensions right away, but they become extremly useful the more you write your code.
Now that we learned what are list coprehensions, let's look at dictionary coprehensions which are very similar in syntax but allows us to create dictionaries in a similar manner.
Dictionary Coprehensionā
When creating a new dictionary using dictionary comprehension, you can perform various operations using expressions to determine the data (key and/or value) that will be stored in the new dictionary.
Syntax for this looks like:
{key: value for (key,value) in iterable}
To demonstrate this, let's imagine that you are building a currency converter. You would maybe have a dictionary representing prices in USD and need to convert them to EUR. In traditional for-loop you would do something like this:
To use dict coprehension we would rewrite the above code to:
round() function rounds the numbers decimal point to specified number of places. It takes in float and a number of decimal places - an integer.
In the above code we use EUR_CONV_RATE to declare constant. Constants are just variables, but are not supposed to be changed during running of your program. They are useful for declaring things that would not change during runtime of your program, and its a convention in Python to write them in ALL_CAPS. Unlike some other languages, in Python, these are considered just like regular variables and Python will not stop you from changing them during runtime, so you need to consider this when writing your application.
The golden rule is:
- If the variable will change during your application runtime, its just a variable and should be written as
variable_name. - If the variable will not change during your application runtime, then you can consider it a constant and write them as
VARIABLE_NAME.
Remember that this is just a convention and it is not a rule you must follow.
Now that we covered dictionary coprehensions, we can finally meet set coprehensions.
Set Coprehensionā
Set comprehension works best when you want a clean transformation and you also want duplicates to disappear without extra effort. The syntax for set coprehension is:
{expression for item in iterable}
For example, let's use our squared example from before:
We can also use conditionals to get only specific values:
Documentation Hunting
Answer These Questionsā
Exerciseā
Assignmentā
What's Nextā
Coprehensions are very useful in every day life as a Python programmer, but there is one thing that is universal accross all languages, so let's start a new chapter; code organization. First thing to learn are functions which enable us to write modular code.