What do those brackets mean? A lesson on indexing in python.

What do those brackets mean? A lesson on indexing in python.

Let’s set the scene, I’m in week one of a Python-based data science bootcamp. I know the math, but I have never coded in Python before. We are given a lab that is built on indexing through dictionaries and lists. But I don’t even know enough coding to know that is called indexing. I ended up sitting next to my roommate for about an hour and talked about what each bracket meant. Fast forward ten weeks, I no longer even have to think when indexing, it comes so naturally.

This blog is for week one Maurie. Hopefully, this helps anyone else in week one of learning Python. It gets better! You can do it!

Let’s use the example that was given to me to help us understand indexing a nested dictionary.

General Assembly has campuses across the country and has global lectures a few times a day. The class times are set by coast and campus.

class_lecture_times = {
    'WC': {
        'LA': {
            'M': [9, 2],
            'T': [9, 2],
            'W': [9, 2],
            'Th': [9],
            'F': [9]},
        'SEA': {
            'M': [9, 2],
            'T': [9, 2],
            'W': [9, 2],
            'Th': [9],
            'F': [9]},
        'DEN': {
            'M': [10, 3],
            'T': [10, 3],
            'W': [10, 3],
            'Th': [10],
            'F': [10]},
        'SF': {
            'M': [9, 2],
            'T': [9, 2],
            'W': [9, 2],
            'Th': [9],
            'F': [9]}},
    'EC': {
        'BOS': {
            'M': [10, 3],
            'T': [10, 3],
            'W': [10, 3],
            'Th': [10],
            'F': [10]}}
    }

If we want to find the time of the second lecture on the Denver campus on Wednesday. we would have to use a series of brackets.

The first one accesses the first level of the dictionary. Dictionaries can be identified by the curly brackets. The keys are the strings (text inside of quotes ‘WC’, ‘DEN’, etc) before the colon. When we are working with a dictionary, we can use the keys to access the items. You can identify if you are working with a dictionary by checking its type:

type(class_lecture_times['EC'])

If you get back “dict” then you can use the keys.

To get all the campuses on the West Coast (‘WC’):

class_lecture_times['WC']

Then we add another bracket to access Denver (‘DEN’):

class_lecture_times['WC']['DEN']

We then need to add another bracket for Wednesday (‘W’):

class_lecture_times['WC']['DEN']['W']

This will give you [10,3]. This is a list. You can identify that this is a list because of the square brackets. Lists are changeable but are ordered. The elements within the lists are called items.

In order to access the second lecture time, we need to talk about indexing the list. Lists are indexed by calling the position of the item in the list. An important Python note! In a list in Python the first item in the list is 0, not one. So in the example above 10 is 0 and 3 is 1.

So in order to access the second item in the class time list, we use this code.

class_lecture_times['WC']['DEN']['W'][1]

This will give you ‘3’.

Got that? It wasn’t so bad!

Now for a little more advanced:

Sometimes you find yourself with a bunch of dictionaries inside of a list. (This is often how JSON is written). You can use the list indexing for the list of dictionaries too. So, if our example class time dictionary was the first dictionary on a larger list, to access the second lecture time in Denver. I would use this

class_lecture_times[0]['WC']['DEN']['W'][1]

The zero is the position of our lecture time dictionary, the WC is the West Coast dictionary, the DEN is the Denver lecture times, the W is Wednesday and the 1 is the second item on the class times list.

I hope this helps all the week one Mauries out there. Indexing is something that seems daunting at first but will become second nature soon.


To view or add a comment, sign in

Others also viewed

Explore content categories