๐Ÿ“– Beginning Python Programming /
Module: List & Collection

Slicing

๐Ÿ“– Beginning Python Programming / List & Collection / Slicing

More slicing example

# Given a sample list
sample_list = [1,2,3,4,5,6,7,8,9]

# First item
print(sample_list[0])

# Last item
print(sample_list[-1])

# Item at index 1โ€”3 (index begins with 0)
print(sample_list[1:4])

# Item at index 1, 3
print(sample_list[1:4:2])

# Every second item from beginning to end
print(sample_list[::2])

# A copy of the whole list
print(sample_list[:])

# Items from beginning to last 5th item.
print(sample_list[:-5])

# Items from the last 3rd to the end.
print(sample_list[-3:])

# A copy of the whole list, but in reversed order
print(sample_list[::-1])