PYTH-1

Lists, indexing and slicing

Python for DSALesson 1 of 612 min

This station is a short start. It covers the parts of Python that the rest of the course uses. If you already write Python every day, read the warnings in each lesson and move on to CORR.

What a list is

A list holds values in order. You write it with square brackets, and you separate the values with commas:

a = [3, 11, 19, 25, 36, 47, 58, 70]
names = ["Asha", "Ravi", "Meera"]
empty = []
len(a)      # 8, the number of values

A list can hold any kind of value, and it can hold a mix of kinds. Most lists in this course hold numbers or strings (text). The function len gives the number of values in a list.

Indexing from the front and the back

Each value in a list has a position, called its index. The first index is 0, not 1. So in a list of 8 values, the indices go from 0 to 7. You read one value with square brackets after the name: a[0] is the first value.

A negative index counts from the end. a[-1] is the last value and a[-2] is the one before it. Python turns a[-k] into a[len(a) - k].

Step through the figure. The blue plates above the cells are the indices, and the train under the list shows which index is being read. The last step reads one past the end. That is an error, and it is the most common index mistake in this whole course.

FIG PYTH-1.1Reading a list by index and by slice
Show
3
11
19
25
36
47
58
70
len(a) 8result —
1/7

This list has 8 values. The blue plates show each value's index, its position in the list. The first index is 0, so the last is 7.

Step 1 of 7

Slicing

A slice copies part of a list into a new list. a[start:stop] takes the values from index start up to, but not including, index stop. Switch the figure to “Slicing” to see each kind.

a[2:6]      # [19, 25, 36, 47]   indices 2, 3, 4, 5
a[:3]       # [3, 11, 19]        from the start
a[5:]       # [47, 58, 70]       to the end
a[::2]      # [3, 19, 36, 58]    every second value
a[::-1]     # the whole list, reversed

Why does the slice stop before stop? Because then the length of a[start:stop] is simply stop − start, and a[:k] and a[k:] split the list into two parts with nothing missing and nothing repeated. You will see this kind of range again in binary search, where it is called a half-open window.

Changing a list

Lists can be changed after they are made. These are the changes you will use most:

a[0] = 4          # replace the value at index 0
a.append(80)      # add 80 at the end
a.pop()           # remove the last value and return it
a.insert(0, 1)    # put 1 at index 0; every other value moves right
a.pop(0)          # remove the first value; every other value moves left

The first three are fast. The last two are slow on long lists, because every value after the change has to move one place. ARRS-1 explains why.

Two names, one list

In Python, a name does not hold a list. It refers to a list. So b = a does not copy anything. It gives the same list a second name.

a = [1, 2, 3]
b = a           # b refers to the same list as a
b.append(4)
print(a)        # [1, 2, 3, 4]   a changed too

c = a[:]        # a slice of everything: a new list
c.append(5)
print(a)        # [1, 2, 3, 4]   a did not change

This matters in the course. When a function receives a list and changes it, the caller sees the change. Sometimes that is what you want, as in sorting a list in place. Sometimes it is a bug. Lesson 3 comes back to this.

PYTH-1 counts as completed once you step through the figure to the last step.