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

bookList Indexing

In Python, lists allow for element indexing. This means we can access each item in the list by its index. Remember, indexing in lists starts at 0. So, the first item is at index 0, the second at index 1, and so forth.

1234567
list_A = ['red', 'green', 'blue', 'yellow', 'black'] # Getting the first element print(list_A[0]) # Getting the third element print(list_A[2])
copy

We've covered positive indexing, but there's also something called negative indexing. Negative indexing starts from the end of the list. For instance, index -1 refers to the last item, index -2 points to the second-to-last item, and so on.

1234567
list_A = ['red', 'green', 'blue', 'yellow', 'black'] # Getting the last element print(list_A[-1]) # Getting the fourth element print(list_A[3], list_A[-2])
copy

Let's put this into practice.

Task

You're given: list_1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']. Your task is to extract the first, third, fifth, and seventh items from this list and create a new list with these items, using positive indices only.

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 4
toggle bottom row

bookList Indexing

In Python, lists allow for element indexing. This means we can access each item in the list by its index. Remember, indexing in lists starts at 0. So, the first item is at index 0, the second at index 1, and so forth.

1234567
list_A = ['red', 'green', 'blue', 'yellow', 'black'] # Getting the first element print(list_A[0]) # Getting the third element print(list_A[2])
copy

We've covered positive indexing, but there's also something called negative indexing. Negative indexing starts from the end of the list. For instance, index -1 refers to the last item, index -2 points to the second-to-last item, and so on.

1234567
list_A = ['red', 'green', 'blue', 'yellow', 'black'] # Getting the last element print(list_A[-1]) # Getting the fourth element print(list_A[3], list_A[-2])
copy

Let's put this into practice.

Task

You're given: list_1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']. Your task is to extract the first, third, fifth, and seventh items from this list and create a new list with these items, using positive indices only.

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 4
toggle bottom row

bookList Indexing

In Python, lists allow for element indexing. This means we can access each item in the list by its index. Remember, indexing in lists starts at 0. So, the first item is at index 0, the second at index 1, and so forth.

1234567
list_A = ['red', 'green', 'blue', 'yellow', 'black'] # Getting the first element print(list_A[0]) # Getting the third element print(list_A[2])
copy

We've covered positive indexing, but there's also something called negative indexing. Negative indexing starts from the end of the list. For instance, index -1 refers to the last item, index -2 points to the second-to-last item, and so on.

1234567
list_A = ['red', 'green', 'blue', 'yellow', 'black'] # Getting the last element print(list_A[-1]) # Getting the fourth element print(list_A[3], list_A[-2])
copy

Let's put this into practice.

Task

You're given: list_1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']. Your task is to extract the first, third, fifth, and seventh items from this list and create a new list with these items, using positive indices only.

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!

In Python, lists allow for element indexing. This means we can access each item in the list by its index. Remember, indexing in lists starts at 0. So, the first item is at index 0, the second at index 1, and so forth.

1234567
list_A = ['red', 'green', 'blue', 'yellow', 'black'] # Getting the first element print(list_A[0]) # Getting the third element print(list_A[2])
copy

We've covered positive indexing, but there's also something called negative indexing. Negative indexing starts from the end of the list. For instance, index -1 refers to the last item, index -2 points to the second-to-last item, and so on.

1234567
list_A = ['red', 'green', 'blue', 'yellow', 'black'] # Getting the last element print(list_A[-1]) # Getting the fourth element print(list_A[3], list_A[-2])
copy

Let's put this into practice.

Task

You're given: list_1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']. Your task is to extract the first, third, fifth, and seventh items from this list and create a new list with these items, using positive indices only.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt