Sign in

Intro to Computer Science / Unit 8: Lists and Data

8.2 Changing Lists

3 practice questionsNot yet reviewed

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 list is 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?
What does a = [5, 6, 7]; x = a.pop(); print(x) display?
What does print(sum([3, 5, 2]) / len([3, 5, 2])) calculate?

Ready for more? Take the Unit 8 test.