Lists

Lists are objects that let you hold on to multiple values at once in a sane and organized fashion.

Introduction

Lists are ordered collections of objects. Objects in lists can be of any type. You can also add and remove list entries. They are indicated by "[" and "]" brackets.

my_list = [1,2,3]

Indexing

Predict what this code does.


In [ ]:
some_list = [10,20,30]
print(some_list[2])

In [ ]:
some_list = [10,20,30]
print(some_list[0])

In [ ]:
some_list = [10,20,30]
print(some_list[-1])

Summarize

How do you access elements in a list?

Predict what this code does.


In [ ]:
some_list = [10,20,30,40]
print(some_list[1:3])

In [ ]:
some_list = [10,20,30]
print(some_list[:3])

Summarize

What does the ":" symbol do?

Modify

Change the cell below so it prints the second through fourth elements in the list.


In [ ]:
some_list = [0,10,20,30,40,50,60,70]
print(some_list[2:4])

Setting values in lists

Predict what this code does.


In [ ]:
some_list = [10,20,30]
some_list[0] = 50
print(some_list)

Predict what this code does.


In [ ]:
some_list = []
for i in range(5):
    some_list.append(i)
print(some_list)

Predict what this code does.


In [ ]:
some_list = [1,2,3]
some_list.insert(2,5)
print(some_list)

In [ ]:
some_list = [10,20,30]
some_list.pop(1)
print(some_list)

In [ ]:
some_list = [10,20,30]
some_list.remove(30)
print(some_list)

Summarize

How can you change entries in a list?

Implement

Write a program that creates a list with all integers from 0 to 9 and then replaces the 5 with the number 423.


In [ ]:

Miscellaneous List Stuff


In [ ]:
# You can put anything in a list
some_list = ["test",1,1.52323,print]

In [ ]:
# You can even put a list in a list
some_list = [[1,2,3],[4,5,6],[7,8,9]] # a list of three lists!

In [ ]:
# You can get the length of a list with len(some_list)
some_list = [10,20,30]
print(len(some_list))

Copying lists

(a confusing point for python programmers)

Predict what this code does.


In [ ]:
some_list = [10,20,30]

another_list = some_list
some_list[0] = 50

print(some_list)
print(another_list)

Predict what this code does.


In [ ]:
import copy

some_list = [10,20,30]

another_list = copy.deepcopy(some_list)
some_list[0] = 50

print(some_list)
print(another_list)

Think about it for a moment. What might be going on?

DANGER

The previous cells demonstrate a common (and confusing) python gotcha when dealing with lists.

In the first case:

The statement

another_list = some_list

says that another_list is some_list. These are now two labels for the same underlying object. It does not create a new object. If I change some_list, it changes another_list because they are the same thing. In programming terms, another_list and some_list are both references to the same object.

I can write:

Mary is also known as Jane.

I can then use "Mary" or "Jane" and it will be understood both labels point to the same person. If I say, "Jane has red hair", it implies that "Mary" also has red hair because they are the same person.

In the second case:

The statement

another_list = copy.deepcopy(some_list)

says to make a copy of some_list and call it another_list. These are now independent. I can modify one without modifiying another. (In programming terms, another_list and some_list now refer to different objects).

In our Mary and Jane analogy, the sentence would be:

I cloned Mary and named the clone Jane.

Now the two labels point to to different people. If I say "Jane has red hair" it does not imply that "Mary has red hair" (Mary may have dyed her hair blue).

Summary

  • Python lists start at 0 and count to N-1.
  • Negative numbers (starting at -1) count from the right side.
  • ":" lets you slice lists.
    • Using some_list[i:j+1] returns the $i^{th}$ through the $j^{th}$ entries in the list.
    • some_list[:3] gives the $0^{th}$ through the $2^{nd}$ entries.
    • some_list[1:-1] gives the $1^{st}$ through the last entries.
    • some_list[:] gives the whole list
  • Change $i^{th}$ entry to $x$ by some_list[i] = x
  • Append $x$ to the end by some_list.append(x)
  • Insert $x$ at the $i^{th}$ position by some_list.insert(i,x)
  • Remove the $i^{th}$ entry by some_list.pop(i)
  • Remove the first entry with value $x$ by some_list.remove(x)

If you want to copy a list, do:

import copy

some_list = [1,2,3]
list_copy = copy.deepcopy(some_list)