Python Zip Exercises Let’s check out some exercises that will help understand zip() function better. Exercise 12-a: Merging 2 Lists with Zip Function Using zip() function and list() function, create a merged list of tuples from the two lists given. lst1=[19542209, 4887871, 1420491, 626299, 1805832, 39865590] lst2=["New York", "Alabama", "Hawaii", "Vermont", "West Virginia", "California"] #Type your answer here. lst3= print(lst3) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(lst3,list(zip(lst1, lst2)),"lst3 checks") myTests().main() Hint 1 zip(a, b) can be used to merge two lists together and it returns a list of tuples. Hint 2 list() function can be used to create lists from iterators that’s created by functions such as range, map, zip, filter… Solution lst3 = list(zip(lst1, lst2)) Exercise 12-b: Merging a List and a Range with Zip Function First create a range from 1 to 8. Then using zip, merge the given list and the range together to create a new list of tuples. lst1=["Energy", "Agriculture", "Industry", "Technology", "Finance", "Forestry", "Transport"] #Type your answer here. rng1= lst= print(lst) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(lst,zip(lst1, rng1),"lst checks") myTests().main() Hint 1 range(1,8) will give you the range you need. Hint 2 list() function can be used to create lists from iterators that’s created by functions such as range, map, zip, filter… Solution rng1 = list(range(1,8))lst = zip(lst1, rng1) Exercise 12-c: Creating a Dictionary by Merging 2 Lists Using Zip Function Using zip and dict functions create a dictionary which has its key-value pairs coming from lst1 and lst2. lst1=["Netflix", "Hulu", "Sling", "Hbo"] lst2=[198, 166, 237, 125] #Type your answer here. points= print(points) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(points,dict(zip(lst1, lst2)),"lst checks") myTests().main() Hint 1 You can use Python’s built-in zip function and put it inside dict function to create a dictionary.dict(zip()) Hint 2 zip(lst1, lst2) will create tuples from lst1 and lst2. Solution dict(zip(lst1,lst2)) Exercise 12-d: 2 Lists Zip Merged and Sorted Using zip, list and sorted functions create a sorted list of tuples from lst1 and lst2. lst1=["Mike", "Danny", "Jim", "Annie"] lst2=[4, 12, 7, 19] #Type your answer here. sorted_lst= print(sorted_lst) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(sorted_lst,sorted(list(zip(lst1,lst2))),"lst checks") myTests().main() Hint 1 You can merge 2 lists with zip function. zip(lst1,lst2) Hint 2 You can convert a zip object to a proper Python list using the list function.list(zip(lst1,lst2)) Hint 3 All that’s needed is to put everything inside a sorted() function to create a sorted list. Solution sorted(list(zip(lst1,lst2)))