I have the following code
d1={'key1':[1,2,3],'key2':[4,5,6]}
d1['key2'][0][2]
which produces this error:
TypeError: 'int' object is not subscriptable
I want the output to be 4,6
Join Stack Overflow to learn, share knowledge, and build your career.
I have the following code
d1={'key1':[1,2,3],'key2':[4,5,6]}
d1['key2'][0][2]
which produces this error:
TypeError: 'int' object is not subscriptable
I want the output to be 4,6
You are using subscription operation on a list item which is an integer (4 in this case), hence the error.
You can get your desired output using list slicing:
In [193]: d1 = {'key1': [1,2,3], 'key2': [4,5,6]}
In [194]: d1['key2']
Out[194]: [4, 5, 6]
In [195]: d1['key2'][0::2] # [start:stop:step]
Out[195]: [4, 6]
d1['key2'][0:3]
on current example will get you [4]
.
– heemayl
Apr 13 '19 at 10:42
d1['key2'][0]
return4
, and you cannot subscriptint
, ergo the mentioned error – han solo Apr 13 '19 at 10:24