Bharath Pabba Bharath Pabba - 1 month ago 8
Python Question

'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)

Answer Source

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