Data Structures

A data structure is a way to store and organize data in a computer program. Lists and a dictionaries are both data structures. In this lesson you’ll see how you can combine dictionaries and lists to make complex structures that represent real-world data. Then you’ll see how you can retrieve from the web using Python.

Lists and Dictionary Values

List and dictionary values can be any type in Python. That’s right, they can be strings, number, file handles or even other lists or dictionareis. Creating a dictionary of dictionaries is easy, where you would have put a scalar value you put the declaration of a dictionary. For example, think about a contact in your phone:

contact = {
    'Name': 'Mike',
    'Email': 'mike@mike.com',
    'Phone': {
        'Work': '555-1212',
        'Mobile': '555-2323',
    }
}

Look closely and you’ll see that the value for the key Phone is itself a dictionary. Enter the example into the cell below and print it:

[ ]:

How do you access the phone numbers? You stack together the square brackets [ and ]. Here’s how you would print Mike’s work number:

print(contact['Phone']['Work'])

Try that:

[ ]:

Set a breakpoint on that print statement and look at the contact variable in the debugger. See how the values nest? When you want to get a value from the structure you have to remember to dereference the contact information from left to right.

Looping Over Data Structures

The for loop doesn’t change when you start to loop over more complicated structures, but it’s a little tricky to keep track of everything at first. Let’s see a more complicated example where you can see how to loop over contact information. Here’s an example of a contact list:

contacts = {
    'Mike': {
        'Name': 'Mike',
        'Email': 'mike@mike.com',
        'Phone': {
            'Work': '555-1212',
            'Mobile': '555-2323',
        }
    },
    'Nora': {
        'Name': 'Nora',
        'Email': 'nora@nora.com',
        'Phone': {
            'Work': '555-2222',
            'Mobile': '555-3333',
        }
    }
}

Copy the example into the next cell and add yourself as a contact:

[ ]:

Now let’s build loops to show all of the information in the contact list. Let’s start by looping over contact names:

for contact in contacts:
    print(f"Name: {contact}")

Not so hard! Enter that into the next cell and test it:

[ ]:

Now let’s print out more information about each contact:

for contact in contacts:
    print("Name:", contact)
    print("  Email:", contacts[contact]["Email"])

Now update your code to print the email address:

[ ]:

Make sure you understand how email is being retrieved. Here’s an explainer:

  • contacts – The contact list (a dictionary with contact names as keys)

  • contact – A key from the dictionary

  • contacts[contact] – A dictionary with contact information types as keys

  • contacts[contact]["Email"] – The email address of contact.

But how do we get the phone numbers? We need another for loop! Here’s completed code that gets all of the information:

for contact in contacts:
    print("Name:", contact)
    print("  Email:", contacts[contact]["Email"])
    for phonetype in contacts[contact]["Phone"]:
        print("  Phone:", phonetype, contacts[contact]["Phone"][phonetype])

Complete your code in the next cell:

[ ]:

Now let’s add to the explanation the way we got the phone number information:

  • contacts[contact]["Phone"] – A dictionary with phone types as keys and numbers as values

  • phonetype – A key in the contacts[contact]["Phone"] dictionary

  • contacts[contact]["Phone"][phonetype] – The phone number corresponding to phonetype.

This is complicated for a beginner. With practice you’ll get the hang of it. Use the debugger.

Web APIs

If data structures are complicated, why learn them in an intro class? Because knowing them enables you to do one of the most important and interesting things you can do with Python: Use the web to load data into your program. Click this link to Wikipedia:

https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Lion

It’s not the Wikipedia site you’re expecting! What you see on your screen is JavaScript Object Notation or (JSON). It’s compact, so I’ve “unrolled” it for you below:

{
    "batchcomplete": "",
    "query": {
        "pages": {
            "36896": {
                "pageid": 36896,
                "ns": 0,
                "title": "Lion",
                "extract": "The lion (Panthera leo) is a species in the family Felidae..."}
        }
    }
}

Look familiar? JSON and Python notation for dictionaries are almost identical. In most cases you can copy JSON right into your Python program and it will work. (One key eception is that JSON uses lower case t and f in True and False.) However you don’t need to copy and paste data, Python has packages that allow you to fetch a website and convert JSON into native Python dictionaries. Here’s a function that queries Wikipedia and returns the JSON data as a Python dictionary:

[22]:
import json
import requests
import urllib

def wiki_fetch(title) :
    """Queries Wikipeida using the JSON API and returns the resulting page.

    Arguments:
        title - (string) The title of the page.

    Returns:
        The JSON response converted to a dictionary.
    """
    safe_title = urllib.parse.quote(title)
    url = f'https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles={safe_title}'
    response = requests.get(url)
    data = json.loads(response.text)
    return data

Here’s an example of how you call the function:

data = wiki_fetch("Lion")

Call the function and print the data that is returned.

[ ]:

Use the debugger to look through the data.