Exercises

  1. Sort the following string alphabetically, from z to a, and assign it to the variable sorted_letters.

1.1 Sort the list, lst from largest to smallest. Save this new list to the variable lst_sorted.

1.2 Sort the list below, animals, into alphabetical order, a-z. Save the new list as animals_sorted.

  1. The dictionary, medals, shows the medal count for six countries during the Rio Olympics. Sort the country names so they appear alphabetically. Save this list to the variable alphabetical.

2.1 Sort the following dictionary based on the keys so that they are sorted a to z. Assign the resulting value to the variable sorted_keys.

2.2 Below, we have provided the dictionary groceries, whose keys are grocery items, and values are the number of each item that you need to buy at the store. Sort the dictionary’s keys into alphabetical order, and save them as a list called grocery_keys_sorted.

  1. Given the same dictionary, medals, now sort by the medal count. Save the three countries with the highest medal count to the list, top_three.

3.1 Sort the following dictionary based on the value from highest to lowest. Assign the resulting value to the variable sorted_values.

3.2 Once again, we have provided the dictionary groceries. Once again, you should return a list of its keys, but this time they should be sorted by their values, from highest to lowest. Save the new list as most_needed.

  1. Create a function called last_four that takes in an ID number and returns the last four digits. For example, the number 17573005 should return 3005. Then, use this function to sort the list of ids stored in the variable, ids, from lowest to highest. Save this sorted list in the variable, sorted_ids. Hint: Remember that only strings can be indexed, so conversions may be needed.

4.1 Sort the following list by each element’s second letter a to z. Do so by creating a function called second_let for the key. Assign the resulting value to the variable func_sort.

4.2 Below, we have provided a list of strings called nums. Write a function called last_char that takes a string as input, and returns only its last character. Use this function to sort the list nums by the last digit of each number, from highest to lowest, and save this as a new list called nums_sorted.

  1. Sort the list ids by the last four digits of each id. Do this using lambda and not using a defined function. Save this sorted list in the variable sorted_id.

5.1 Sort the following list by each element’s second letter a to z. Do so by using lambda. Assign the resulting value to the variable lambda_sort.

5.2 Once again, sort the list nums based on the last digit of each number from highest to lowest. However, now you should do so by writing a lambda function. Save the new list as nums_sorted_lambda.

  1. Challenge Given is the nested dictionary, pokemon, which shows the pokemon each trainer has caught in the early stages of Pokemon Go. Pool this data together in a dictionary assigned to the variable name, pooled. The pooled dictionary should have the total number of rattatas, eevees, etc. Then, sort the compiled dictionary based on the number of pokemon from greatest number to least number to the variable sorted_pooled. Assign the most common pokemon to the variable common.

6.1 Challenge: Below, we have provided the nested dictionary medals that describes how many medals the USA won in various sports at the Rio Olympics. Write code to sort the sports in medals based on the total number of medals that were won, from highest to lowest. Save the list of sorted sports as sorted_sports. Save the sport with the most medals as most_medals and the sport with the least medals as least_medals.

6.2 Challenge Here is a dictionary called pokemon_go_data that contains 4 trainers and their data about which pokemon they have caught and how many candy they have for each one. Compress the data so that there is just one dictionary that has all of the information on how many candy each pokemon has overall. Sort this dictionary and assign to the variable popular_pokemon the top 5 pokemon (those who have the most amount of candy).

Next Section - Extra Exercises