What is the output of the following? a = [1, 2, 3] print(a[-1], a[-2:])

Python Developer Medium

Python Developer — Medium

What is the output of the following? a = [1, 2, 3] print(a[-1], a[-2:])

Key points

  • Negative indexing in Python starts from the end of the list
  • a[-1] accesses the last element of the list
  • a[-2:] accesses a sublist starting from the second-to-last element
  • The output includes the last element and a sublist from the second-to-last element to the end

Ready to go further?

Related questions