Indexing and Slicing

Indexing (1)

Accessing the n-th element is straightforward …

>>> text = "Hello World"
>>> text[0]
'H'
>>> text[6]
'W'
>>> text[-1]
'd'
>>> text[-4]
'o'
>>> text[len(text)-1] == text[-1]  # AAH!!
True

Indexing (2)

Same with other sequences

>>> a_list = ['Peter', 'Paul', 'Mary']
>>> a_list[0]
'Peter'
>>> a_list[-1]
'Mary'
>>> a_tuple = (1, 'one', 1.0)
>>> a_tuple[0]
1
>>> a_tuple[-1]
1.0

Slicing: Cutting Out

Extracting part of a sequence

>>> text = "Hello World"
>>> text[0:5]
'Hello'
>>> text[:5]
'Hello'
>>> text[6:11]
'World'
>>> text[6:]
'World'
>>> text[6:-1]
'Worl'
>>> text[-5:-1]
'Worl'

Slicing: Step Width

Killer feature: slices with step width

>>> text = "Hello World"
>>> text[0:7:2]
'HloW'
>>> text[::2]
'HloWrd'
>>> text[:-6:2]
'Hlo'
>>> text[::-1]
'dlroW olleH'

Slice Assignment

Sub-slice assignment

>>> l = [2, 3, 'a', 'b', 7]
>>> l[2:4] = [4, 5, 6]
>>> l
[2, 3, 4, 5, 6, 7]

Prepending

>>> l[:0] = [0, 1]
>>> l
[0, 1, 2, 3, 4, 5, 6, 7]

Appending (but see list methods append() and extend()

>>> l[len(l):] = [8, 9]
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]