Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
List Indexing | List
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set

book
List Indexing

In Python, lists allow you to access individual elements using their index. Indexing starts at 0, meaning the first element in a list is at index 0, the second element is at index 1, and so on. This is called zero indexing. To access an element, use square brackets with the index of the desired item.

1234567
cities = ["Rome", "London", "New York", "Brasilia", "Kioto"] # Getting the first element print(cities[0]) # Getting the third element print(cities[2])
copy

In the next example, the index 0 returns the first element, 'Rome', while the index 2 returns the third element, which is 'New York'. Remember that the index corresponds to the position minus one (n - 1).

Negative Indexing

Python also supports negative indexing: this allows you to access elements from the end of the list. Here, -1 represents the last item, -2 represents the second last item, and so on. Negative indexing can be very useful when you want to work with a list from the end without knowing its length.

1234567
cities = ["Rome", "London", "New York", "Brasilia", "Kioto"] # Getting the last element print(cities[-1]) # Getting the fourth element print(cities[3], cities[-2])
copy

Explanation:

  1. -1 retrieves the last element 'Kioto';
  2. 3 and -2 both refer to the fourth element 'Brasilia', one using positive indexing and the other using negative indexing. This demonstrates how indexing wraps around the list.

Positive and negative indexing add versatility to the access of elements from lists, either from the beginning or the end.

Indexing in Nested Lists

Accessing elements in a nested list requires multiple indices: the first index selects the sublist, and the second index accesses the specific item within that sublist.

1234567891011121314
cities = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500] ] # Accessing the first sublist print(cities[0]) # Output: ['Paris', 'France', 2000] # Accessing the city name in the second sublist print(cities[1][0]) # Output: Tokyo # Accessing the cost of the trip in the third sublist print(cities[2][2]) # Output: 2500
copy

Applications of nested list indexing include but are not limited to, structured data such as spreadsheets, matrices, or databases. Practical examples could be accessing rows and columns in a 2D matrix, retrieving details from lists of employee records, or extracting specific information, such as the city names or costs from travel itineraries or nested JSON-like structures.

Task
test

Swipe to show code editor

You are given a list of cities, and your task is to retrieve:

  • The second element in the list using its index;
  • The last element in the list using negative indexing.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 2
toggle bottom row

book
List Indexing

In Python, lists allow you to access individual elements using their index. Indexing starts at 0, meaning the first element in a list is at index 0, the second element is at index 1, and so on. This is called zero indexing. To access an element, use square brackets with the index of the desired item.

1234567
cities = ["Rome", "London", "New York", "Brasilia", "Kioto"] # Getting the first element print(cities[0]) # Getting the third element print(cities[2])
copy

In the next example, the index 0 returns the first element, 'Rome', while the index 2 returns the third element, which is 'New York'. Remember that the index corresponds to the position minus one (n - 1).

Negative Indexing

Python also supports negative indexing: this allows you to access elements from the end of the list. Here, -1 represents the last item, -2 represents the second last item, and so on. Negative indexing can be very useful when you want to work with a list from the end without knowing its length.

1234567
cities = ["Rome", "London", "New York", "Brasilia", "Kioto"] # Getting the last element print(cities[-1]) # Getting the fourth element print(cities[3], cities[-2])
copy

Explanation:

  1. -1 retrieves the last element 'Kioto';
  2. 3 and -2 both refer to the fourth element 'Brasilia', one using positive indexing and the other using negative indexing. This demonstrates how indexing wraps around the list.

Positive and negative indexing add versatility to the access of elements from lists, either from the beginning or the end.

Indexing in Nested Lists

Accessing elements in a nested list requires multiple indices: the first index selects the sublist, and the second index accesses the specific item within that sublist.

1234567891011121314
cities = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500] ] # Accessing the first sublist print(cities[0]) # Output: ['Paris', 'France', 2000] # Accessing the city name in the second sublist print(cities[1][0]) # Output: Tokyo # Accessing the cost of the trip in the third sublist print(cities[2][2]) # Output: 2500
copy

Applications of nested list indexing include but are not limited to, structured data such as spreadsheets, matrices, or databases. Practical examples could be accessing rows and columns in a 2D matrix, retrieving details from lists of employee records, or extracting specific information, such as the city names or costs from travel itineraries or nested JSON-like structures.

Task
test

Swipe to show code editor

You are given a list of cities, and your task is to retrieve:

  • The second element in the list using its index;
  • The last element in the list using negative indexing.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt