Press "Enter" to skip to content

Accessing Elements in Python

Python List Indexing: Accessing Elements with the Correct Index 

In Python, lists are ordered collections of items, and each item is assigned a unique index based on its position in the list. It’s important to note that list indexes in Python start from 0, not 1. This means that the first element in a list has an index of 0, the second element has an index of 1, and so on.

So, if we have a list list1 with the elements [2, 4, 5, 7, 2], and we want to access the fourth element, which is 7, we need to use the index 3, not 4. This is because the index of the first element is 0, the index of the second element is 1, and so on, until we reach the fourth element, which has an index of 3.

Therefore, the correct way to access the fourth element of list1 is by using the index 3, like this: list1[3].

Here’s an academic reference that supports this explanation:

In the book “Python for Data Science Handbook” by Jake VanderPlas, Chapter 2, Section 2.2 “Lists” explains how Python lists work and how to access their elements using indexes. It specifically mentions that Python list indexes start from 0, and that the first element in a list has an index of 0. The book is available online for free at the following link: https://jakevdp.github.io/PythonDataScienceHandbook/02.02-the-basics-of-numpy-arrays.html#Lists

In addition, the official Python documentation also confirms that list indexes start from 0. The documentation can be found at this link: https://docs.python.org/3/tutorial/introduction.html#lists 

Wiki Hyphen Website | Updates 15th March 2023 | Link: https://www-wiki.com/Accessing-Elements-in-Python

Mission News Theme by Compete Themes.