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. lst1=[1,2,3,4,5] #Type your answer here. lst2= print(lst2) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(lst2,[i for i in lst1], "lst2 checks") myTests().main() Hint 1 You can type your list comprehension inside brackets: [] Solution lst2 = [i for i in lst1] Exercise 16-b Create a list from the elements of a range from 1200 to 2000 with steps of 130, using list comprehension. #Type your answer here. rng= lst=[] print(lst) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(lst, [1200, 1330, 1460, 1590, 1720, 1850, 1980], "lst checks") myTests().main() Hint 1 You can type your list comprehension inside brackets: [] Hint 2 range can be constructed by using 3 parameters:range(start,stop,step) Solution 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. #Type your answer here. lst1=[44,54,64,74,104] lst2=[] print(lst2) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(lst2, [50, 60, 70, 80, 110], "lst2 checks") myTests().main() Hint 1 You can type your list comprehension inside brackets: [] Hint 2 You can start your list comprehension with [i+6 for i…] Solution lst2 = [i+6 for i in lst1] Exercise 16-d Using list comprehension, construct a list from the squares of each element in the list. lst1=[2, 4, 6, 8, 10, 12, 14] #Type your answer here. lst2=[] print(lst2) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(lst2, [4, 16, 36, 64, 100, 144, 196], "lst2 checks") myTests().main() Hint 1 You can type your list comprehension inside brackets: [] Hint 2 i**2 is the correct syntax to get the square of a number in Python. Solution lst2 = [i**2 for i in lst1] Exercise 16-e Using list comprehension, construct a list from the squares of each element in the list, if the square is greater than 50. lst1=[2, 4, 6, 8, 10, 12, 14] #Type your answer here. lst2=[] print(lst2) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(lst2, [64, 100, 144, 196], "lst2 checks") myTests().main() Hint 1 You can type your list comprehension inside brackets: [] Hint 2 i**2 is the correct syntax to get the square of a number in Python. Solution lst2 = [i**2 for i in lst1 if i**2>50] Exercise 16-f Given dictionary is consisted of vehicles and their weights in kilograms. Contruct a list of the names of vehicles with weight below 5000 kilograms. In the same list comprehension make the key names all upper case. dict={"Sedan": 1500, "SUV": 2000, "Pickup": 2500, "Minivan": 1600, "Van": 2400, "Semi": 13600, "Bicycle": 7, "Motorcycle": 110} #Type your answer here. lst=[] print(lst) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(lst, ['SEDAN', 'SUV', 'PICKUP', 'MINIVAN', 'VAN', 'BICYCLE', 'MOTORCYCLE'], "lst checks") myTests().main() Hint 1 You can type your list comprehension inside brackets: [] Hint 2 [i.upper() for i…] can be a good start for the comprehension. Hint 3 dict[i] can be used to access the value of the key: i in a dictionary named dict Solution lst = [i.upper() for i in dict if dict[i]<5000]