Python Tutorials
Basic

Data Structure and Data Types

String

List

Tuple

Set

Dictionary

Functions / Methods

Advance

Exception Handling

Python Access List Items


Access List Items

The list items are indexed. The first item of the list index position is 0. You can access list items by using the index position with square brackets ([]).

Example
colors_list = ["Red", "Blue", "Green"]
print(colors_list[1])
Blue

In the above example, we declared a colors list (colors_list) and added some color names to the list. Using the index position, we are currently accessing the first list item ([1]). The first index position item in this list is "Blue".


Negative Indexing in Python

When you use negative indexing in Python, item picking starts from the end of the list.

You can access items from the end, meaning that if you put index [-1], you get the last item from the list, and if you put index [-2], you get the second-last item from the list.

Example
colors_list = ["Red", "Blue", "Green"]
print(colors_list[-1])
Green

In the above example, we use negative indexing to access the last item ([-1]) from the list. In this case, "Green" is the last item on the list.

You can access the second-last ("Blue") item of the list by using negative indexing [-2].


Range of Indexes

Range of Indexes is also called Slicing.

In the slicing, you can specify where to start and where to end, and when you do that, it returns a new list with items between the start and end indexes.

Example
colors_list = ["Red", "Blue", "Green", "Orange", "White", "Black", "Yellow"]
print(colors_list[1:5])
['Blue', 'Green', 'Orange', 'White']

python-list-slicing

In the above example, we set the start index range from 1, and the end index is 5, resulting in a new list with the following items (1) "Blue", (2) "Green", (3) "Orange", (4) "White". The output includes the index position 1 ("Blue") item but excludes the index position 5 ("Black") item.

Whenever you perform a slicing, it includes the start position item and excludes the end position item. Remember that the index position of the first item is 0.


Range of Indexes Specifies Only the Start Position

In the slicing, when you specify where to start and ignore the end item position, automatically it goes to the end of the list.

Example
colors_list = ["Red", "Blue", "Green", "Orange", "White", "Black", "Yellow"]
print(colors_list[3:])
['Orange', 'White', 'Black', 'Yellow']

python-list-slicing

In the above example, we specify the start item position (3) and ignore the end item position so that the new list will cover all items after the start item index from the original list (["Orange", "White", "Black", "Yellow"]).


Range of Indexes Specifies Only the End Position

In the slicing, when you ignore where to start and specify only the end item position, the indexes will begin from the first item and goes up to the specified index position.

Example
colors_list = ["Red", "Blue", "Green", "Orange", "White", "Black", "Yellow"]
print(colors_list[:4])
['Red', 'Blue', 'Green', 'Orange']

python-list-slicing

In the above example, we ignore the start item position and specify the end item position (4) so that the new list will cover all items before the end item index from the original list (["Red", "Blue", "Green", "Orange"]).


Range of Negative Index

In the slicing with negative indexes, you can specify where to start and where to end (negative indexing starts picking items from the end), and when you do that, it returns a new list with items between the start and end indexes.

Example
colors_list = ["Red", "Blue", "Green", "Orange", "White", "Black", "Yellow"]
print(colors_list[-5:-2])
['Green', 'Orange', 'White']

python-list-slicing-with-negative-indexing

In the above example, the negative index position for the start item is -5, and the end index position is -2, resulting in a new list with the following items (-5) "Green", (-4) "Orange", (-3) "White". The output includes the index position -5 ("Green") item but excludes the index position -2 ("Black") item.


Check item exists in the list

Check if the specified item exists in the list by using the "in" keyword.

Example
colors_list = ["Red", "Blue", "Green"]
if "Blue" in colors_list:
    print("Blue exists in the list.")
else:
    print("Blue does not exist in the list.")
Blue exists in the list.

In the above example, we use the "in" keyword to check whether the "Blue" color exists in the list. Here, the "Blue" color exists in the list, so the output statement is "Blue exists in the list.".