How to access List elements in Python?

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.

Advertisements

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top