Skip to content

Accessing list items

Accessing items in a list

List items can also be accessed using indexing and slicing.

Using indexing

accessing list items
numbers = [3, 6, 9]
# Accessing list items
print(numbers[0]) # output: 3
print(numbers[2]) # output: 9
print(numbers[1]) # output: 6

Using slicing

slicing to access a subset of list items
# a list containing 8 numbers
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
# Accessing the last four numbers
print(my_list[3:])