Lists

Lists

You can access an item in a list using an index:

my_list = [0, 0, 2, 0, 0, 1]

What’s the index for value 2 and 1?

You can replace an item in a list using an index:

my_list = [0, 0, 2, 0, 0, 1]
my_list[1] = 5
my_list
[0, 5, 2, 0, 0, 1]

Lists

Here is the list:

my_list = [0, 0, 2, 0, 0, 1]

How do we move value 1 to where value 2 is, replacing 1 with zero so the list looks like this:

my_list = [0, 0, 1, 0, 0, 0]

Swapping list values

We want to go from this:

my_list = [0, 0, 2, 0, 0, 1]

To this:

my_list = [0, 0, 1, 0, 0, 0]

We do this:

index_a = 2
index_b = 5
my_list[index_a] = my_list[index_b]
my_list[index_b] = 0

Short Project

Instructions for Short Project of the Week