EN
Python - access list items
0 points
In this article, we would like to show you how to access list items in Python.
Quick solution:
xxxxxxxxxx
1
my_list = ['A', 'B', 'C']
2
3
print(my_list[0]) # A
4
print(my_list[-3]) # A
In this example, we use positive indexing to access list items by refering to the specific index number. We start from index 0
that refers to the first item.
xxxxxxxxxx
1
my_list = ['A', 'B', 'C']
2
3
print(my_list[0]) # A
4
print(my_list[1]) # B
5
print(my_list[2]) # C
Output:
xxxxxxxxxx
1
A
2
B
3
C
In this example, we use negative indexing to access list items by refering to the specific index number. We start from index -1
that refers to the list item.
xxxxxxxxxx
1
my_list = ['A', 'B', 'C']
2
3
print(my_list[-1]) # C
4
print(my_list[-2]) # B
5
print(my_list[-3]) # A
Output:
xxxxxxxxxx
1
C
2
B
3
A