Operations on Elements in List Comprehension in Python
In the inclusion to the left of the for...in construction, you can specify not only a variable, but also set a specific operation for it.
Let's make a list where one is subtracted from each generated element:
lst = [i - 1 for i in range(1, 5)]
print(lst)
After executing the code, a new list will be returned:
[0, 1, 2, 3]
What will be the result of running the following code:
lst = [i + 2 for i in range(0, 6)]
print(lst)
What will be the result of running the following code:
lst = [i / 2 for i in range(4, 10)]
print(lst)
What will be the result of running the following code:
lst = [i + 10 for i in range(0, 8, 2)]
print(lst)