[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
Example:
Function Parameters are Aliases:
Another Example:
5. Copying Lists
Copy vs Alias
Other ways to copy
6. Destructive and Non-destructive Functions
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)
8. Adding Elements video
Destructively (Modifying Lists)
Non-Destructively (Creating New Lists)
Destructive vs Non-Destructive Example
9. Removing Elements
Destructively (Modifying Lists)
Non-Destructively (Creating New Lists)
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
Destructively with list.sort() or list.reverse()
Non-Destructively with sorted(list) and reversed(list)
More list methods
12. Tuples (Immutable Lists)
Tuple syntax
Tuples are immutable
Parallel (tuple) assignment
Singleton tuple syntax
13. List Comprehensions
14.Converting Between Lists and Strings
Last updated