Intro to Computer Science / Unit 8: Lists and Data
8.2 Changing Lists
Notes
Lists have methods to add, remove and reorder items.
append(x)adds x to the end.insert(i, x)adds x at index i.remove(x)deletes the first x.pop()removes and returns the last item.len(list),sum(list),min(list),max(list)summarize a list of numbers.x in listis True if x is present.
pets = ["cat", "dog"]
pets.append("fish")
pets.remove("cat")
print(pets)
- This prints
['dog', 'fish'].
Practice
What does
a = [1, 2]; a.append(3); print(len(a)) display?After appending, the list has 3 items.
What does
a = [5, 6, 7]; x = a.pop(); print(x) display?pop() removes and returns the last item.
What does
print(sum([3, 5, 2]) / len([3, 5, 2])) calculate?Sum divided by count is the mean: 10 / 3.
Ready for more? Take the Unit 8 test.