16.9. Chapter Assessment¶
Sort the following string alphabetically, from z to a, and assign it to the variable sorted_letters
.
Sort the list below, animals
, into alphabetical order, a-z. Save the new list as animals_sorted
.
Write code to rearrange the strings in the list winners
so that they are in alphabetical order by first name from A to Z.
Write code to switch the order of the winners
list so that it is now Z to A, by first name. Assign this list to the variable z_winners
.
Write code to switch the order of the winners
list so that it is now A to Z by last name. Assign this list to the variable z_winners
.
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
.
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
.
We have provided the dictionary groceries
. You should return a list of its keys, but they should be sorted by their values, from highest to lowest. Save the new list as most_needed
.
Create a function called last_four
that takes in a single ID number and returns the last four digits. For example, the number 17573005 should return 3005. Then, use the resulting 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.
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
.
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
.