More on Strings and Lists, Slicing, Mutability

CS 65: Introduction to Computer Science I

Group Exercise

Run the following lines of code one-by-one in your interactive shell, and discuss what each of them is doing.

In [7]:
my_list = [0,10,20,30,40,50,60,70,80,90]
print( my_list[2:7] )
print( my_list[2:] )
print( my_list[:7] )
my_string = "The quick brown fox jumps over the lazy dog"
print( my_string[12:25] )
print( my_list.index(30)  )
print( my_string.index("lazy")  )
lazy_index = my_string.index("lazy") 
print(my_string[lazy_index:(lazy_index+4)])
[20, 30, 40, 50, 60]
[20, 30, 40, 50, 60, 70, 80, 90]
[0, 10, 20, 30, 40, 50, 60]
own fox jumps
3
35
lazy

This is called slicing.

Notice that my_list[start:end] gives you the slice of the list starting at index start and ending just before index end.

Slicing to make a copy

Recall that lists only reference their data, so when you assign, it copies the reference to the same list - you end up with two names for the same list.

In [1]:
x = [1,2,3,4,5]
y = x
y[2] = 9999
print(x)
print(y)
[1, 2, 9999, 4, 5]
[1, 2, 9999, 4, 5]

But, a slice will always be a copy, so you can make a real copy of a whole list using a slice

In [2]:
x = [1,2,3,4,5]
y = x[:]
y[2] = 9999
print(x)
print(y)
[1, 2, 3, 4, 5]
[1, 2, 9999, 4, 5]

Strings vs. Lists

sequences, can use [] notation, can loop through them both, can check their length with len()

In [8]:
my_list = [0.0, 1.1, 42, 3.14]
my_string = "The quick brown fox jumps over the lazy dog"

print( my_list[2] )
print( my_string[2] )
42
e

work with concatenation + and repition * operators

In [9]:
print( my_list + [1,2,3] )
print( my_string + ", and the dog doesn't care." )
print( my_list * 3 )
print( my_string * 3 ) 
[0.0, 1.1, 42, 3.14, 1, 2, 3]
The quick brown fox jumps over the lazy dog, and the dog doesn't care.
[0.0, 1.1, 42, 3.14, 0.0, 1.1, 42, 3.14, 0.0, 1.1, 42, 3.14]
The quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dog

can process them with in, count(), and index()

  • for lists, this works with items
  • for strings, this works with any substring
In [ ]:
print( 1.1 in my_list )
print( "fox" in my_string )
print( my_list.count(42) )
print( my_string.count("ox") )
print( my_list.index(42) )
print( my_string.index("ox") )

Group Exercise:

Before running these, discuss in your groups what you think will happen. Then, try it and see if you were right.

In [23]:
my_list = [0.0, 1.1, 42, 3.14]
my_list[2] = 5
print(my_list)

my_string = "The quick brown fox jumps over the lazy dog"
#my_string[2] = "y"
#print(my_string)
my_string_list = list(my_string)
print(my_string_list)
my_string_list[2] = "y"
print(my_string_list)
my_string =  "".join(my_string_list) 
print(my_string)
[0.0, 1.1, 5, 3.14]
['T', 'h', 'e', ' ', 'q', 'u', 'i', 'c', 'k', ' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x', ' ', 'j', 'u', 'm', 'p', 's', ' ', 'o', 'v', 'e', 'r', ' ', 't', 'h', 'e', ' ', 'l', 'a', 'z', 'y', ' ', 'd', 'o', 'g']
['T', 'h', 'y', ' ', 'q', 'u', 'i', 'c', 'k', ' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x', ' ', 'j', 'u', 'm', 'p', 's', ' ', 'o', 'v', 'e', 'r', ' ', 't', 'h', 'e', ' ', 'l', 'a', 'z', 'y', ' ', 'd', 'o', 'g']
Thy quick brown fox jumps over the lazy dog

How are strings and lists different?

You might think that a string is just a list of characters, and that's pretty close. However, they differ in one important property:

Lists are mutable, meaning they can be changed

Strings are immutable, meaning they can't be changed

This means that with a string, you can't use any list method that changes the list

  • append()
  • insert()
  • remove()
  • pop()

We will see other examples of both mutable and immutable objects in Python.