You can access the list items using following techniques:
1. Using index():
To access the element in any list one can refer to its index number.
List=[1,2,3,4,5]
# Print element at index 3
print(List[3])
Output: 4
2. Range of Indexes:
We can specify a range of indexes by specifying where to start and where to end the range. When specifying a range, the return value will be a new list with the specified items.
List=[1,2,3,4,5]
print(List[2:4])
Output: [3,4]
3. Negative Indexing
Negative indexing means beginning from the end, -1
refers to the last item, -2
refers to the second last item etc.
List=[1,2,3,4,5]
print(List[-1])
Output: 5
- Log in to post comments