filmov
tv
List Comprehensions in Python.
Показать описание
How to write better code in python with list comprehensions?
Welcome to Tekminded!
in this video we will review how to simplify your python code and make it more readable using list comprehensions.
Before we start if you have not yet subscribed our channel do not forget to hit the subscribe button.
Also if you like this video please remember to press the like button to help us promote the channel to more viewers like you.
As programmers advance in their python journey, a common question that arises is what arer list comprehensions, why use them and when should then be used.
In this video we will attempt to respond each one of these questions.
1)What are List comprehensions:
List comprehensions generally serve as a more concise and pythonic way of writing loops. Let's suppose you want to create a list with the first 10 numbers. One way to do this is to create an empty list and the append each one of the numbers in a loop.
# list comprehensions
integers=[ ]
for i in range(10):
integers
out [1]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
This looks fine but there's a better way to do that using comprehensions.
You can assign the list directly to the variable and automatically generate the list!
In this case, for each element in the range from 0 to 10 which would use the indexes zero to nine, the list created is incremented by the generated number.
Once the command in this line is completed, we can retrieve back the list and confirm that it contains the numbers from 0 to 9.
# lists
integers=[i for i in range(10)]
integers
out [2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
We just saw how to create a list using a comprehension but the same technique works for sets dictionaries and tuples sets. here we created a set very similar to the list we created in the previous example.
# sets
integers={i for i in range(10)}
integers
out [3]: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Let's see an example using directonaries:
# dictionary
integers_double={i:i*2 for i in range(10)}
integers_double
out [4]: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}
We can also use other iterables such as strings. Here we just converted the string python into a list containing the letters of the word python:
# strings
string_var = 'Python'
x = [i for i in string_var]
print(x)
['P', 'y', 't', 'h', 'o', 'n']
We can also create tuples however with tuples you have to explicitly use the tuple function to prevent python from confusing the round brackets of the tuple with a regular round bracket and creating a generator instead:
# tuples
integers=tuple(i for i in range(10))
integers
out [6]: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
Now that we already saw a few examples of list comprehensions, let's review the general structure to build one. In general a list comprehension has three components and noted below
General structure of a list comprehension¶
- EXPRESSION is member, method, or any other valid expression that returns a value.
- MEMBER: is the object or value in the list or iterable.
- ITERABLE: is a list, set, sequence, generator, or any other object that can return its elements one at a time.
Let's review again our comprehension that creates a list with the range method:
# list
integers=[i for i in range(10)]
integers
We have i which is the member AND the expression. What if we want to make the expression more complex?
We absolutely can !!! Let's say we want to get the square of the first 10 numbers? The comprehension below will do the job:
# List with an expression
integers=[i*i for i in range(10)]
integers
out [8]: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
We can also have a conditional expression. Let's say that for numbers that are lesser than three we want to assign a value of minus one. We just need to create a conditional where we get i if the member is greater than three and if not, we assign the minus 1 as the value to be placed in our list:
# list with a conditional expression
integers=[i if i "greater than operator" 3 else -1 for i in range(10)]
integers
Note: The "greater than operator" is not allowed in Youtube comments so we replaced it with words.
[out [9]: [-1, -1, -1, -1, 4, 5, 6, 7, 8, 9]
One last example. The interval can also contain a conditional clause. let's say we only want to add even numbers to our list. In this case our iterable will have a conditional IF, including only numbers that when divided by 2 have a module of 0.
# list with a conditional interable
integers=[i for i in range(10) if i%2==0]
integers
[out [9]: [0, 2, 4, 6, 8]
As you can see list comprehensions in Python are very powerful and can greatly simplify your code.
It is very common for them to be used within lambda functions. If we want to learn more about lambda functions, please check out this quick video.
I hope you enjoyed this video. Do no forget to leave your comments, like and subscribe to be informed about more videos like this one!!!
Welcome to Tekminded!
in this video we will review how to simplify your python code and make it more readable using list comprehensions.
Before we start if you have not yet subscribed our channel do not forget to hit the subscribe button.
Also if you like this video please remember to press the like button to help us promote the channel to more viewers like you.
As programmers advance in their python journey, a common question that arises is what arer list comprehensions, why use them and when should then be used.
In this video we will attempt to respond each one of these questions.
1)What are List comprehensions:
List comprehensions generally serve as a more concise and pythonic way of writing loops. Let's suppose you want to create a list with the first 10 numbers. One way to do this is to create an empty list and the append each one of the numbers in a loop.
# list comprehensions
integers=[ ]
for i in range(10):
integers
out [1]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
This looks fine but there's a better way to do that using comprehensions.
You can assign the list directly to the variable and automatically generate the list!
In this case, for each element in the range from 0 to 10 which would use the indexes zero to nine, the list created is incremented by the generated number.
Once the command in this line is completed, we can retrieve back the list and confirm that it contains the numbers from 0 to 9.
# lists
integers=[i for i in range(10)]
integers
out [2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
We just saw how to create a list using a comprehension but the same technique works for sets dictionaries and tuples sets. here we created a set very similar to the list we created in the previous example.
# sets
integers={i for i in range(10)}
integers
out [3]: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Let's see an example using directonaries:
# dictionary
integers_double={i:i*2 for i in range(10)}
integers_double
out [4]: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}
We can also use other iterables such as strings. Here we just converted the string python into a list containing the letters of the word python:
# strings
string_var = 'Python'
x = [i for i in string_var]
print(x)
['P', 'y', 't', 'h', 'o', 'n']
We can also create tuples however with tuples you have to explicitly use the tuple function to prevent python from confusing the round brackets of the tuple with a regular round bracket and creating a generator instead:
# tuples
integers=tuple(i for i in range(10))
integers
out [6]: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
Now that we already saw a few examples of list comprehensions, let's review the general structure to build one. In general a list comprehension has three components and noted below
General structure of a list comprehension¶
- EXPRESSION is member, method, or any other valid expression that returns a value.
- MEMBER: is the object or value in the list or iterable.
- ITERABLE: is a list, set, sequence, generator, or any other object that can return its elements one at a time.
Let's review again our comprehension that creates a list with the range method:
# list
integers=[i for i in range(10)]
integers
We have i which is the member AND the expression. What if we want to make the expression more complex?
We absolutely can !!! Let's say we want to get the square of the first 10 numbers? The comprehension below will do the job:
# List with an expression
integers=[i*i for i in range(10)]
integers
out [8]: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
We can also have a conditional expression. Let's say that for numbers that are lesser than three we want to assign a value of minus one. We just need to create a conditional where we get i if the member is greater than three and if not, we assign the minus 1 as the value to be placed in our list:
# list with a conditional expression
integers=[i if i "greater than operator" 3 else -1 for i in range(10)]
integers
Note: The "greater than operator" is not allowed in Youtube comments so we replaced it with words.
[out [9]: [-1, -1, -1, -1, 4, 5, 6, 7, 8, 9]
One last example. The interval can also contain a conditional clause. let's say we only want to add even numbers to our list. In this case our iterable will have a conditional IF, including only numbers that when divided by 2 have a module of 0.
# list with a conditional interable
integers=[i for i in range(10) if i%2==0]
integers
[out [9]: [0, 2, 4, 6, 8]
As you can see list comprehensions in Python are very powerful and can greatly simplify your code.
It is very common for them to be used within lambda functions. If we want to learn more about lambda functions, please check out this quick video.
I hope you enjoyed this video. Do no forget to leave your comments, like and subscribe to be informed about more videos like this one!!!