Exercise 16: List Comprehension | HolyPython.com Best way to practice. (original) (raw)
Python List Comprehension Exercises
Let’s check out some exercises that will help understand List Comprehension better.
Exercise 16-a
Create an identical list from the first list using list comprehension.
You can type your list comprehension inside brackets: []
Exercise 16-b
Create a list from the elements of a range from 1200 to 2000 with steps of 130, using list comprehension.
You can type your list comprehension inside brackets: []
range can be constructed by using 3 parameters:
range(start,stop,step)
rng = range(1200, 2000, 130)
lst = [i for i in rng]
Exercise 16-c
Use list comprehension to contruct a new list but add 6 to each item.
You can type your list comprehension inside brackets: []
You can start your list comprehension with [i+6 for i…]
lst2 = [i+6 for i in lst1]
Need More Exercises?
Check out Holy Python AI+ for amazing Python learning tools.
*Includes 14 more programming languages, inspirational tools and 1-on-1 consulting options.
Umut Sagir
Finance & Data Science Professional,
Founder of HolyPython