Bharath Pabba
Bharath Pabba

Reputation: 1855

'set' object has no attribute '__getitem__'

I have a dictionary name typecontext and I have a list named types. I want to take five elements of types and add it to the dict typecontext. I am getting a error that set attribute has no object getitem.

 try:
    k=0
    typecontext = dict()
    for i in range(0, len(types), 5):
        print(i)
        if i + 5 < len(types):
            typecontext.update({'item_'+str(k) :types[i:i + 5]})
        else:
            typecontext.update({'item_' + str(k): types[i:len(types)]})
        k=k+1
except Exception as e:
    print(e)

Upvotes: 1

Views: 673

Answers (2)

damisan
damisan

Reputation: 1047

Take a look at the islice function. It works with any iterable.

Upvotes: 2

Seer.The
Seer.The

Reputation: 487

It looks like your variable types is a set object. So when you try to slice it you get your error.

Upvotes: 3

Related Questions