Skip to main content

Python: Access List items

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

Tags
Submitted by devanshi.srivastava on June 9, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.