0

I am trying to sublist an list which contains long.

a = [ -846930886, -1714636915, 424238335, -1649760492]
print(a[2:1])

This returns []. What's happening? I could find only this way of sub listing.

Netwave
  • 40,134
  • 6
  • 50
  • 93
tanvi
  • 927
  • 5
  • 17
  • 32

2 Answers2

3

a[2:1] is not a valid slicing and will return empty list.

The correct syntax is object[start:end:inteval]. If you want to traverse in backward you should add interval

>>> print(a[2:1:-1])
[424238335]

Another approach will be with passing single index

>>> print(a[-2])
424238335

Or if you want to traverse in forward direction use :

>>> print(a[1:2])
[-1714636915]
akash karothiya
  • 5,736
  • 1
  • 19
  • 29
1

Your sublist starts after it ends.So,

print(a[1:2]) 

will give you

[-1714636915].
Naresh Kumar
  • 1,706
  • 1
  • 14
  • 26
DanWheeler
  • 188
  • 10