= [2, 4, 'resonant', -1.2] L
Week 8: Lists
Reading: Think Python Chapter 10
Notes
Sequences
- Lists are sequences.
- Lists can contain a mix of different types
- Much of what you learned about strings as sequences is also true of lists.
- Lists are delimited with square brackets:
- Lists are indexed with square brackets, identical to string indexing:
2] L[
'resonant'
1:3] L[
[4, 'resonant']
- Unlike strings, lists are mutable. Values can be assigned in to lists:
1] = 'dampened' L[
L
[2, 'dampened', 'resonant', -1.2]
The len
function also works for lists:
len(L)
4
Operators, Functions, Methods
Operators work with lists somewhat similarly to how they work with strings:
+
between two lists concatenates the lists and returns a new list
4, "3"] + [2, 1] [
[4, '3', 2, 1]
- A list can be “multiplied” with an integer using
*
0] * 6 [
[0, 0, 0, 0, 0, 0]
Lists have built-in methods, similar to strings. One of the most useful of these is <list>.append()
= ['apple', 'pear', 'mango'] fruit
'tomato') fruit.append(
fruit
['apple', 'pear', 'mango', 'tomato']
<list>.sort()
will sort numeric values as you would expect:
[1, 2, 5, 6, 10]
It sorts strings and characters a bit differently:
alpha_sort,py
[' CHICKEN', 'Banana', 'Pear', 'apple', 'mango']
What happened? In Python, every character is associated with an integer. This mapping is known as ASCII. Functions ord()
and chr()
convert between characters and numbers.
ord('A')
65
ord('b')
98
ord('@')
64
chr(99)
'c'
Comparing and sorting strings/characters uses these values. You will never need to memorize them.
Iterating
Iterating through lists is very similar to iterating through strings:
There are some differences, largely because lists are mutable:
Why was no 8
printed?
Very importantly when you iterate directly through the elements of a list with content iteration, the looping variable is a temporary copy of the list element. Changing that temporary variable does not change the list!
On the other hand, using value iteration, you can directly access and change list elements.
References
When a ‘primitive’ type is assigned to a variable, the value is assigned directly to the variable. When a list is assigned to a variable, the variable becomes a reference to the list. If two variables are assigned to the same list, changing one variable effectively changes the other- since you are changing the list that both variables reference.
Here’s an example:
You can check if two variables reference the same list with the is
keyword:
= [1, 2] a
= [1, 2] b
= a c
== b a
True
is b a
False
is c a
True
The ==
operator between lists checks if the contents of two lists are the same, the is
keyword checks if two variables reference the same list. This a subtle, but important difference.
Lists and Functions
Similarly, passing a list as an argument to a function passes a reference to this list. If your function modifies the referenced list inside the function, the list is modified outside the function as well.
Note how the add_to_list
function modified the list without returning anything.
Practice
Practice Problem 8.1
Practice Problem 8.2
Practice Problem 8.3
Practice Problem 8.4
Practice Problem 8.5
Homework
Homework problems should always be your individual work. Please review the collaboration policy and ask the course staff if you have questions.
Double check your file names and return values. These need to be exact matches for you to get credit.
Homework Problem 8.1
Homework Problem 8.2
Homework Problem 8.3
Homework Problem 8.4
Homework Problem 8.5
- Find the shortest string:
- Visit each element in the list
- Use the
len()
function to check lengths - Use a variable to remember the shortest length you have found
- Use a second variable to remember the index associated with that length
- Update both variables if you find a new shorter length
- Now that you know where the string is (the index you found):
- Create a new string, the reverse of the original
- Assign it into the list in the correct position