Practice Mode - Dictionaries

Screen Link:

My Code:

def remove_rows(dic, del_index):
    for i in del_index:
        for j in dic.items()[0]:
            dic[j].pop(i)
    return dic

What I expected to happen:
I expect to get solution

What actually happened:

"Executing the function remove_rows caused an error: TypeError: 'dict_items' object is not subscriptable."

You get that error because the [0] part of dic.items()[0] is incorrect.

Let’s say you have the following -

a = [1, 2, 3]

a is a list, as you know. The variable a is an object of type list.

Similarly, dic.items() is an object of type dict_items.

In Python, you can index a list such as a[0], but you can’t index dict_items. Therefore, your code of dic.items()[0] throws that error. It’s not a valid operation in Python.

So, you will have to use an alternative approach tot accomplish whatever you were trying to do.

1 Like