0

Whenever I print a list of values in negative indexing in the console it shows only empty brackets. Whatever is the reason behind this problem image is attached

stu_information = ["name","roll_number","address","contact_information"]
print(stu_information[-1:-2])

Output:

[]
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69

1 Answers1

1

Your slice of list is always empty:

k = [1,2,3,4]

print( k[-1:-2] )  # this is an empty slice

print( k[-2:-1] )  # this is a non empty slice

You slice from -1 as start (4) to -2 as end (3) which you can not do unless you go backwards through the list:

[1, 2, 3, 4]
#        -1
#     -2
#   -3
#-4
    

... slicing from -1 to -2 only works backwards ...

print( k[-1:-2:-1] )  # this is NOT an empty slice because you move backwards through it

Output:

[]
[3]   # the later index is not included 
[4]   #   hence only 1 output sliced
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69