List Comprehensions in Python

List comprehensions allow you to generate lists by applying a given expression to each element of the future list. Comprehensions can also be used to filter elements according to specified conditions.

The comprehension specifies the expression that will be applied to the elements of the original list. The for...in construct specifies the element name and the source list (iterable object) from which the new one will be created:

pythonlist = [expression for element in iter]

Let's declare a variable lst. For it, we specify that the list element i should be generated in the range from 1 to 10. Then we output the resulting list to the console:

pythonlst = [i for i in range(1, 10)]
print(lst)

After executing the code, a new list will be returned:

python[1, 2, 3, 4, 5, 6, 7, 8, 9]
Задание 1

Using a comprehension, create the following list:

python[1, 2, 3, 4, 5]
Задание 2

Using a comprehension, create the following list:

python[5, 4, 3, 2, 1]