This notebook provides practice problems associated with lesson 2. It is divided into several parts:

  • Part 1 - most approachable place to start. Practices syntax and concepts

  • Part 2 - a step up from part 1. Integrates concepts from accross the lesson in more applied scenarios

Lists and Dictionaries Practice#

Part 1#

Lists#

  1. Create a list with 4-6 items that represents some foods.

  1. Write a line of code to figure out how many foods are in your list. Write a print statement which displays Number of foods is: ___ where the __ space is calculated based on the number of items in your list.

  1. 🐞 Debugging: Find and fix the error in the following block of code.

pollutants = ['co" 'co2', 'no2', 'o3']
  Cell In[1], line 1
    pollutants = ['co" 'co2', 'no2', 'o3']
                                        ^
SyntaxError: unterminated string literal (detected at line 1)

Indexing#

  1. For each of the bullets, print out the color from the colors list using indices.

  • red

  • blue, yellow, white (for a challenge, use just 1 line of code)

colors = ['red', 'green', 'blue', 'yellow', 'white', 'black']
  1. 🐞 Debugging: The goal of the following block of code is to print out the value o3. Find and fix the error.

pollutants = ['co', 'co2', 'no2', 'o3']
print(pollutants[4])
----------------------------------------------------------------
IndexError                     Traceback (most recent call last)
<ipython-input-7-ba5a7280c547> in <module>
      1 pollutants = ['co', 'co2', 'no2', 'o3']
----> 2 print(pollutants[4])

IndexError: list index out of range

Functions/Methods#

  1. Returning to our list of pollutants, let’s say we learned that no2 is the wrong value. Instead we want that item to be nox. Update the pollutants list below so the output is ['co', 'co2', 'nox', 'o3'].

pollutants = ['co', 'co2', 'no2', 'o3']
  1. This article linked in the lecture describes a few common list methods. Scroll down to where the article talks about pop() and apply that to our pollutants list by removing ‘no2’ from the list.

pollutants = ['co', 'co2', 'no2', 'o3']

Dictionaries#

  1. Create a dictionary that has an item for each of the last 4 days and displays the weather condition on each of those days.

  1. Using the dictionary you made in the previous question:

  • print all the keys

  • print all the values

  • pick one specific key and print its value

  1. 🐞 Debugging: Find and fix the error in the following block of code.

metadata = {
    'location': 'Nairobi', 
    'quality_flag': 5
    'zenith': 60, 
    'clouds': True,
    'data_center': ['LPDAAC', 'ASDC'],
}
  File "<ipython-input-13-4d50fa6ccc43>", line 4
    'zenith': 60,
    ^
SyntaxError: invalid syntax
  1. 🐞 Debugging: The goal of the following block of code is to print out the keys of the dictionary metadata. Find and fix the error.

metadata = {
    'location': 'Nairobi', 
    'quality_flag': 5,
    'zenith': 60, 
    'clouds': True,
    'data_center': ['LPDAAC', 'ASDC'],
}
print(metadata.keys)
<built-in method keys of dict object at 0x7ffe4794eb40>

Adding or changing items#

  1. Using the following dictionary change the value for state to PA.

census_block7 = {
    'state': 'NY',
    'block_groups': ['01', '02', '03', '04'],
    'median_education': 'some_bachelors',
    'total_housing_units': 9329432,
}
  1. Using the census_block7 dictionary add the value 78.2 for a new item called employment_rate.

Data Structures#

Come up with 1-2 fake datasets that make sense as a list and type out an example list. Do the same for 1-2 dictionaries.

# Example
max_daily_temperatures_NYC = [56, 50, 49, 49, 60]

Part 2#

Assign the value of a dictionary to another variable

Question 1#

The list below represents the smallest size of particulate matter (PM) measured in a series of air samples. Find the number of occurances of PM2.5 and PM10 in the list.

pm_levels = ['PM1', 'PM2.5', 'PM1', 'PM10', 'PM10', 'PM10', 'PM2.5', 'PM10', 'PM1', 'PM2.5']

Question 2#

One very common practice in dictionaries is called nesting. Nesting is the practice of putting one data structure inside of another data structure. So a nested dictionary is a dictionary inside of another dictionary.

state_info = {
    'state': 'AK',
    'motto': 'north to the future',
    'num_of_counties': 29,
    'population': {
        'ages_0_25': 28393,
        'ages_25_50': 25920,
        'ages_over_50': 8943,
        }
}

A) Print out the following pieces of information from the dictionary above:

  • the motto

  • the population ages 25 to 50

B) Change the population ages 0 to 25 to 98988

C) Add a new age bracket to the population dictionary by adding an additional item: ages_50_75 with the population 3333. Change the item ages_over_50 to be renamed ages_50_75.

Question 3#

Another common example of nested objects is a list of dictionaries. Below we have the same nested dictionary as in question two, but we have a list of three states’ data.

census_by_state = [
    {
        'state': 'AK',
        'motto': 'north to the future',
        'num_of_counties': 29,
        'population': {
            'ages_0_25': 28393,
            'ages_25_50': 25920,
            'ages_over_50': 8943,
            }
    },
    {
        'state': 'FL',
        'motto': 'in god we trust',
        'num_of_counties': 67,
        'population': {
            'ages_0_25': 382933,
            'ages_25_50': 483923,
            'ages_over_50': 938923,
            },
    },
    {
        'state': 'MN',
        'motto': 'letoile du nord',
        'num_of_counties': 87,
        'population': {
            'ages_0_25': 39323,
            'ages_25_50': 109282,
            'ages_over_50': 92837,
        },
    },
]

A) Print the following population numbers:

  • population ages 25-50 in Alaska

  • number of counties in Florida

  • population ages 0-25 in Minnesota

  • motto of alaska

B) Change:

  • the population over age 50 in florida to 8898

  • the motto of minnesota to “go pack go!”

  • the number of counties in alaska to 31

C) Add a new state to the list of dictionaries. Pick whatever state and values you want.

Question 4#

Using the list of values below create new lists made up of the following chunks:

  • The first 5 items in the list

  • The middle third of the list items

  • The first 3 items and the last 3 items together

Question 5#

Write some code that starts with an index and returns the value from the list below at that index. Print the value Include in the print statement if the value is even or odd.

starting_data = [4, 6, 4, 1, 3, 4, 6, 9, 8, 7, 6, 5, 5, 3, 2]
index = 3
# Your code here