0

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

1 Answers1

1

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]
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
heemayl
  • 39,294
  • 7
  • 70
  • 76