Lesson 23 of 4057%

(Progress persistence is disabled until Phase 9)

Python
Beginner

List Methods

Common built-in methods for manipulating lists.

List Methods

What You’ll Learn

You’ll learn the most common methods used to modify lists.

Adding Items

  • append(): Adds an element at the end of the list.
  • insert(): Adds an element at the specified position.
fruits = ["apple", "banana"]
fruits.append("orange")      # ["apple", "banana", "orange"]
fruits.insert(1, "cherry")   # ["apple", "cherry", "banana", "orange"]

Removing Items

  • remove(): Removes the first item with the specified value.
  • pop(): Removes the element at the specified position (or the last item if no index is specified).
fruits.remove("banana")
last = fruits.pop()  # Removes and returns "orange"

Organizing Lists

  • sort(): Sorts the list alphabetically/numerically.
  • reverse(): Reverses the order of the list.
numbers = [4, 1, 9, 3]
numbers.sort()    # [1, 3, 4, 9]
numbers.reverse() # [9, 4, 3, 1]

Lesson Progress

Practice This TopicTake QuizBuild a Python Project

Ready to practise?

Apply what you learned with a hands-on challenge.

Practise This Topic