This article explains how to access single or multiple elements from a list in Python.
A list is a sequential data structure, and all the elements in the List are indexed. Therefore, we can access any element of the List by its index position. Also, the indexing starts from 0 in the List. Let’s understand by an example,
Suppose we have a list of strings,
list_of_names = ['John', 'Mark', 'Jose', 'Shan']
Each element in this List has an index position associated with it i.e.
- Index position of string ‘John’ is 0
- Index position of string ‘Mark’ is 1
- Index position of string ‘Jose’ is 2
- Index position of string ‘Shan’ is 3
Now, let’s see how we can access an element from this List by its index position.
Access nth element in the List
To access an element from the List by its index position, we must pass it in square brackets. Like to access nth element do like this,
list_obj[n-1]
Let’s see how to access 3rd element from a list of strings
Access third element from the list
list_of_names = ['John', 'Mark', 'Jose', 'Shan'] # Get third element from list user = list_of_names[2] print(user)
Output:
Jose
It returned the 3rd element in the List. As indexing starts from 0, so the index position of the third element is 2 here.
Access nth element from last in List using negative indexing
The List also supports negative indexing. Here, the negative index means index position from the end i.e.
Index position of the last element of List is: -1
Index position of 2nd last element of List is: -2
Index position of 3rd last element of List is: -3
Index position of 4rd last element of List is: -4
…..
Index position of nth last element of List is: -n
We can use this negative indexing to access elements from last. Let’s see some examples,
Access last element of List
list_of_names = ['John', 'Mark', 'Jose', 'Shan'] # Get last element from list user = list_of_names[-1] print(user)
Output:
Shan
Access second last element of List
list_of_names = ['John', 'Mark', 'Jose', 'Shan'] # Get second last element from list user = list_of_names[-2] print(user)
Output:
Jose
Access multiple elements from List
Access elements from List using index range
You can select multiple items from a list using index range i.e., start & end index positions. For example,
list_obj[start : end]
It returns a new list containing selected elements from the calling list object, i.e., from start index position to end-1 index position.
Let’s see some examples,
Access elements from index position 2 to 7 in a list i.e., element at index position 2, 3, 4, 5 and 6
list_of_numbers = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] # Access elements from index position 2 to 7 nums = list_of_numbers[2:7] print(nums)
Output:
[12, 13, 14, 15, 16]
Access elements from index position 0 to 2 in a list i.e., element at index position 0 and 1
list_of_numbers = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] # Access elements from index position 0 to 2 nums = list_of_numbers[0: 2] print(nums)
Output:
[10, 11]
Access elements from List using negative index range
List in Python also supports negative index range. It means you can select multiple items from a list at the end i.e., using negative index range i.e. -start & -end index positions. For example,
list_obj[-start : -end]
It returns a new list containing selected elements from the calling list object i.e., from -start index position to -(end-1) index position. Here -n index position means nth element from last.
Let’s see some examples,
Access elements from index position -5 to -2 in a list i.e., element at index position -5, -4, -3 i.e., third last, fouth last, and fifth the last element from List.
list_of_numbers = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] # Access elements at index position -3, -4, -5 nums = list_of_numbers[-5 : -2] print(nums)
Output:
[16, 17, 18]
Access elements from index position -3 to -1 in a list, i.e., the elements at index position -3 and -2 are the second last and third last element in the List.
list_of_numbers = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] # Access elements at index position -3 and -2 nums = list_of_numbers[-3 : -1] print(nums)
Output:
[18, 19]
Summary:
Today we learned how to access single or multiple elements from a list in Python.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.