KnowledgeBoat Logo
|

Robotics & Artificial Intelligence

What would be the output of following code ?

list1 = [2, 3, 4, 5, 6]
print(list1[-2])
  1. 3
  2. 4
  3. 5
  4. Error

Python List Manipulation

3 Likes

Answer

5

Reason — In negative indexing, the index -1 refers to the last element of the list and -2 refers to the second last element, which is 5 in the given list.

Answered By

2 Likes


Related Questions