111k views
4 votes
Suppose list1 is [2, 33, 222, 14, 25]
what is the result for printing list1[-1]?

1 Answer

4 votes

Final answer:

In Python, using list1[-1], prints the last element of list1; in this case, it prints 25.

Step-by-step explanation:

In Python, indexing starts from 0, so list1[0] would be 2, list1[1] would be 33, list1[2] would be 222, list1[3] would be 14, and list1[4] would be 25. Negative Indexing is used to in Python to begin slicing from the end of the string i.e. the last. Slicing in Python gets a sub-string from a string. In Python, list slicing is a common practice and it is the most used technique for programmers to solve efficient problems. Consider a Python list, in order to access a range of elements in a list, you need to slice a list. The slicing range is set as parameters i.e. start, stop, and step. When accessing an element from a list in Python using a negative index, such as list1[-1], the index is counted from the end of the list starting with -1 for the last item. In the list list1 which is [2, 33, 222, 14, 25], using the negative index list1[-1] would return the value of the last element, which is 25.

User Dave Carlile
by
8.9k points