[course]05 —— container
1. Creating Lists
Empty List
print("Two standard ways to create an empty list:")
a = [ ]
b = list()
print(type(a), len(a), a)
print(type(b), len(b), b)
print(a == b)List with One Element (Singleton)
a = [ "hello" ]
b = [ 42 ]
print(type(a), len(a), a)
print(type(b), len(b), b)
print(a == b)List with Multiple Elements
Variable-Length List
2. List Functions and Operations
3. Accessing Elements (Indexing and Slicing)
4. List Mutability and Aliasing
Unlike strings, lists are mutable. This means that they can be changed, without creating a new list. This also forces us to better understand aliases, when two variables reference the same value. Aliases are only interesting (and challenging) for mutable values like lists. Note: it will be especially helpful to use the Visualize feature in the following examples.
Example:
Function Parameters are Aliases:
Another Example:
5. Copying Lists
Copy vs Alias
Other ways to copy
6. Destructive and Non-destructive Functions
Because lists are mutable, we can change them in two ways: destructively (which modifies the original value directly), and non-destructively (which creates a new list and does not modify the original value). This also affects how we write functions that use lists.
Destructive functions
Non-destructive function
7. Finding Elements
Check for list membership: in
Check for list non-membership: not in
Count occurrences in list: list.count(item)
Find index of item: list.index(item) and list.index(item, start)
Example
Problem: crashes when item is not in list
Solution: use (item in list)
8. Adding Elements video
Destructively (Modifying Lists)
Add an item with list.append(item)
Add a list of items with list += list2 or list.extend(list2)
Insert an item at a given index
Non-Destructively (Creating New Lists)
Add an item with list1 + list2
Insert an item at a given index (with list slices)
Destructive vs Non-Destructive Example
9. Removing Elements
Destructively (Modifying Lists)
Remove an item with list.remove(item)
Remove an item at a given index with list.pop(index)
Non-Destructively (Creating New Lists)
Remove an item at a given index (with list slices)
10. Looping Over Lists
Looping with a normal for loop:
Looping with a for each loop
Hazard: modifying inside a for loop
Also Hazard: modifying inside a for-each loop
Better: modifying inside a while loop
11. List Methods: Sorting and Reversing
Lists have many built-in methods. It's common for these methods to be implemented both destructively and non-destructively.
Destructively with list.sort() or list.reverse()
Non-Destructively with sorted(list) and reversed(list)
More list methods
For most list methods, see this table and this list of list methods.
12. Tuples (Immutable Lists)
Tuples are exactly like lists, except they are immutable. We cannot change the values of a tuple.
Tuple syntax
Tuples are immutable
Parallel (tuple) assignment
Singleton tuple syntax
13. List Comprehensions
List comprehensions are a handy way to create lists using simple loops all in one line.
14.Converting Between Lists and Strings
Last updated
Was this helpful?