Extra Exercises

  1. The variable nested contains a nested list. Assign ‘snake’ to the variable output using indexing.

1.1 Below, we have provided a list of lists. Use indexing to assign the element ‘horse’ to the variable name idx1.

1.2 Using indexing, retrieve the string ‘willow’ from the list and assign that to the variable plant.

  1. Below, a list of lists is provided. Use in and not in tests to create variables with Boolean values. See comments for further instructions.

2.1 Below, we’ve provided a list of lists. Use in statements to create variables with Boolean values - see the ActiveCode window for further directions.

2.2 Provided is a nested data structure. Follow the instructions in the comments below. Do not hard code.

  1. The variable nested_d contains a nested dictionary with the gold medal counts for the top four countries in the past three Olympics. Assign the value of Great Britain’s gold medal count from the London Olympics to the variable london_gold. Use indexing. Do not hardcode.

3.1 Below, we have provided a nested dictionary. Index into the dictionary to create variables that we have listed in the ActiveCode window.

3.2 Extract the value associated with the key color and assign it to the variable color. Do not hard code this.

  1. Given the list, nested_d, save the medal count for the USA from all three Olympics in the dictionary to the list US_count.

4.1 Below, we have provided a list of lists that contain information about people. Write code to create a new list that contains every person’s last name, and save that list as last_names.

4.2 Iterate through the contents of l_of_l and assign the third element of sublist to a new list called third.

  1. Given below is a list of lists of athletes. Create a list, t, that saves the only the athlete’s name if it contains the letter “t”. If it does not contain the letter “t”, save the athlete name into lst other.

5.1 Below, we have provided a list of lists named L. Use nested iteration to save every string containing “b” into a new list named b_strings.

5.2 Challenge: Iterate through the list so that if the character ‘m’ is in the string, then it should be added to a new list called m_list. Hint: Because this isn’t just a list of lists, think about what type of object you want your data to be stored in. Conditionals may help you.

  1. Challenge The nested dictionary, pokemon, shows the number of various Pokemon that each person has caught while playing Pokemon Go. Find the total number of rattatas, dittos, and pidgeys caught and assign to the variables r, d, and p respectively. Do not hardcode. Note: Be aware that not every trainer has caught a ditto.

6.1 Challenge: Below, we have provided a nested list called big_list. Use nested iteration to create a dictionary, word_counts, that contains all the words in big_list as keys, and the number of times they occur as values.

6.2 Challenge: Provided is a dictionary that contains pokemon go player data, where each player reveals the amount of candy each of their pokemon have. If you pooled all the data together, which pokemon has the highest number of candy? Assign that pokemon to the variable most_common_pokemon.

Next Section - Introduction: Sorting with Sort and Sorted