TypeError: 'float' object is not subscriptable Dictionaries

Screen Link: https://app.dataquest.io/m/424/dictionaries/13/filtering-for-the-intervals

My Code:

data_sizes = {'0 - 10 MB': 0, '50 - 100 MB': 0, '100 - 500 MB': 0, '500 MB': 0}

for row in apps_data[1:]:
    data_sizes = float(row[2])
    if data_sizes <= 10000000:
        data_sizes['0 - 10 MB'] += 1
        
    elif 10000000 < data_sizes <= 50000000:
        data_sizes['10 - 50 MB'] += 1
    elif 50000000 < data_sizes <= 100000000:
        data_sizes['50 - 100 MB'] += 1
    elif 100000000 < data_sizes <= 500000000:
        data_sizes['100 - 500 MB'] += 1
    elif data_sizes > 5000000000:
        data_sizes['500 MB'] += 1
data_sizes

What I expected to happen:
The dictionary indexes to increment by 1 as appropriate in the conditional statement

What actually happened:
The first time the program attempts to write to the dictionary it fails. The value is a float “389879808.0” from the correct row and the condition works, but the dictionary is not allowing it to be updated by the incrementing…

TypeError                                 Traceback (most recent call last)
<ipython-input-1-3b8ec21cf116> in <module>
     17         data_sizes['50 - 100 MB'] += 1
     18     elif 100000000 < data_sizes <= 500000000:
---> 19         data_sizes['100 - 500 MB'] += 1
     20     elif data_sizes > 5000000000:
     21         data_sizes['500 MB'] += 1

TypeError: 'float' object is not subscriptable

I ran this example as it was presented in the lesson, the only difference between this and the given solution that does work is I am converting to a float, not an int and the indexes are different, but the format is the same and they are both dictionaries. any suggestions? I have run into this error now a few times and it is has always been a typo until now.

You are using the same variable name for the dictionary as well as for storing the value from each row, data_sizes.

Make sure to not have duplicate variable names in your code.

Thank you!

I need to create a little more distinction in my variable naming just to keep things straight in my head.

I also noticed I was missing an index.

1 Like