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
  • 36,219
  • 6
  • 36
  • 71
tanvi
  • 847
  • 4
  • 15
  • 29

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,500
  • 1
  • 16
  • 26
1

Your sublist starts after it ends.So,

print(a[1:2]) 

will give you

[-1714636915].
Naresh Kumar
  • 1,527
  • 1
  • 11
  • 23
DanWheeler
  • 188
  • 10