Python Sort Exercises Let’s check out some exercises that will help understand Python’s .sort() method better. Exercise 11-a Sort the list in ascending order with .sort() method. lst=[11, 100, 99, 1000, 999] #Type your answer here. print(lst) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(lst,[11, 99, 100, 999, 1000],"lst checks") myTests().main() Hint 1 .sort() method will modify the actual list and sort it. Solution lst.sort() Exercise 11-b This time sort the countries in alphabetic order. lst=["Ukraine", "Japan", "Canada", "Kazakhstan", "Taiwan", "India", "Belize"] #Type your code here. print(lst) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(lst,['Belize', 'Canada', 'India', 'Japan', 'Kazakhstan', 'Taiwan', 'Ukraine'],"lst checks") myTests().main() Hint 1 .sort() method will modify the actual list and sort it. Solution lst.sort(reverse = False) Exercise 11-c Now sort the list in descending order with .sort() method. lst=[11, 100, 101, 999, 1001] #Type your answer here. print(lst) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(lst,[1001, 999, 101, 100, 11],"lst checks") myTests().main() Hint 1 .sort() method can be used with “reverse =” parameter. Solution lst.sort(reverse = True) Exercise 11-d Can you sort the gift list in reverse alphabetic order? gift_list=['socks', '4K drone', 'wine', 'jam'] # Type your answer here. print(gift_list) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(gift_list,['wine', 'socks', 'jam', '4K drone'],"lst checks") myTests().main() Hint 1 .sort() method can be used with “reverse =” parameter. Solution gift_list.sort(reverse = True) Exercise 11-e Sort the list below in reverse alphabetic order and then assign the last element to the answer_1 variable. NFL=["Panthers", "Bears", "Dolphins" "Patriots", "Saints", "Giants"] # Type your code here. answer_1= print(answer_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(answer_1,"Bears","answer_1 checks") myTests().main() Hint 1 .sort() method can be used with “reverse =” parameter. Solution NFL.sort(reverse = True)answer_1 = NFL[-1] Exercise 11-f Sort the cities from z to a. muni=["Melbourne", "Shanghai", "Delhi", "Atlanta", "Moscow", "Montreal"] # Type your code here. print(muni) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(muni,['Shanghai', 'Moscow', 'Montreal', 'Melbourne', 'Delhi', 'Atlanta'],"lst checks") myTests().main() Hint 1 .sort() method can be used with “reverse =” parameter. Solution muni.sort(reverse = True) Exercise 11-g Sort the keys of the dictionary from a to z. Hint: You might want to create a list of the keys first with the help of a dictionary method. dict={"tiramisu":5, "chocolate":2, "pudding":3, "cheesecake":4} # Type your code below. key_list= print(key_list) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(key_list,['cheesecake', 'chocolate', 'pudding', 'tiramisu'],"lst checks") myTests().main() Hint 1 .keys method of dictionaries returns all the keys of a dictionary. Hint 2 list() function can be used to construct lists from different types of data. Solution key_list = list(dict.keys())key_list.sort()