What does [3:] mean in reference to [H, E, L, L, O]?

I’m just trying to figure out this problem.

[3:] is a subscript meaning to refers to indices. As shown here, it is known as a slice.

The list, ['H', 'E', 'L', 'L', 'O'] has five elements with indices 0 thru 4.

  0    1    2    3    4
['H', 'E', 'L', 'L', 'O']

Now let’s look at how slices work…

 a = ['H', 'E', 'L', 'L', 'O']
 print (a[3])    # L

The element at index 3 is L. Above we referred to only a single index. With slicing we can access more than one, up to all the elements in the list.

print (a[:])    #  ['H', 'E', 'L', 'L', 'O']

Notice that a slice is also a list, unlike above where we accessed a single element. In that instance we got the value, alone.

The colon in the subscript separates start from end. when no start is specified, Python starts from index 0. and when no end is specified, Python goes to the very end of the list.

start is inclusive, but end is exclusive.

print (a[3:3])    # []

print (a[3:4])    # ['L']

print (a[3:5])    # ['L', 'O']

Even though there is no index 5, it can still be used as end since Python won’t be looking for that element.

Since we don’t have to specify end when we want everything from start to end, we can write,

 print (a[3:])    # ['L', 'O']
1 Like