Skip to main content

List Methods & Operations

Python provides built-in methods to modify, search, sort, and organize items inside a list. Let's learn each method individually with simple code examples.


Part 1: Adding Items to a List

There are 3 main ways to add items to a list: append(), insert(), and extend().

1.1 The append() Method (Add to the End)

The append() method takes a single item and adds it to the very end of the list.

fruits = ["apple", "banana"]

# Add "orange" to the end of the list
fruits.append("orange")

print(fruits)
# Output: ['apple', 'banana', 'orange']

1.2 The insert() Method (Add at a Specific Position)

The insert(index, item) method places a new item at a specific index position, shifting all subsequent items to the right.

fruits = ["apple", "banana"]

# Insert "mango" at index position 0 (the beginning)
fruits.insert(0, "mango")

print(fruits)
# Output: ['mango', 'apple', 'banana']

1.3 The extend() Method (Combine Two Lists)

The extend(another_list) method takes all items from another list and attaches them to the end of the original list.

fruits = ["apple", "banana"]
more_fruits = ["mango", "grapes"]

# Combine more_fruits into fruits
fruits.extend(more_fruits)

print(fruits)
# Output: ['apple', 'banana', 'mango', 'grapes']

Part 2: Removing Items from a List

There are 3 main ways to delete items: remove(), pop(), and clear().

2.1 The remove() Method (Delete by Value)

The remove(value) method searches for an item by its name/value and deletes the first matching occurrence.

fruits = ["apple", "banana", "orange", "banana"]

# Delete the first occurrence of "banana"
fruits.remove("banana")

print(fruits)
# Output: ['apple', 'orange', 'banana']

2.2 The pop() Method (Delete by Index)

The pop(index) method removes the item at a specific index position and returns it. If you don't specify an index, .pop() removes the last item.

fruits = ["apple", "banana", "orange"]

# Remove and return the last item
last_item = fruits.pop()

print("Removed:", last_item) # Output: Removed: orange
print(fruits) # Output: ['apple', 'banana']

# Remove item at index 0
first_item = fruits.pop(0)
print("Removed:", first_item) # Output: Removed: apple
print(fruits) # Output: ['banana']

2.3 The clear() Method (Empty the List)

The clear() method removes all items from the list, leaving it completely empty.

cart = ["laptop", "mouse", "keyboard"]

# Empty the cart completely
cart.clear()

print(cart)
# Output: []

Part 3: Searching & Counting Items

Python provides functions to inspect items inside a list: len(), count(), and index().

3.1 The len() Function (Total Items Count)

len(my_list) returns the total number of items stored inside the list.

scores = [95, 88, 72, 100]

print(len(scores))
# Output: 4

3.2 The count() Method (Count Occurrences)

The count(value) method counts how many times a specific value appears in the list.

numbers = [10, 20, 30, 20, 10, 20]

# Count how many times 20 appears
print(numbers.count(20))
# Output: 3

3.3 The index() Method (Find Position)

The index(value) method returns the index position of the first matching item.

colors = ["red", "green", "blue", "green"]

# Find index position of "blue"
print(colors.index("blue"))
# Output: 2

Part 4: Sorting & Reversing

4.1 The sort() Method (Sort Items in Order)

The sort() method arranges numbers from smallest to largest, or text alphabetically. Adding reverse=True sorts in descending order.

numbers = [5, 1, 4, 2, 3]

# Sort ascending (1 to 5)
numbers.sort()
print(numbers) # Output: [1, 2, 3, 4, 5]

# Sort descending (5 to 1)
numbers.sort(reverse=True)
print(numbers) # Output: [5, 4, 3, 2, 1]

4.2 The reverse() Method (Flip Order)

The reverse() method flips the order of items in the list from back to front.

letters = ["a", "b", "c", "d"]

letters.reverse()

print(letters)
# Output: ['d', 'c', 'b', 'a']

4.3 The copy() Method (Make Backup Copy)

The copy() method creates an independent copy of a list in memory so that modifying the copy does not alter the original.

original = ["red", "blue"]

# Create an independent copy
backup = original.copy()

backup.append("green")

print(original) # Output: ['red', 'blue'] (Unchanged!)
print(backup) # Output: ['red', 'blue', 'green']
Common Beginner Mistakes
  • In-Place Returns None: .sort() and .reverse() modify the list directly in place and return None. Do not write nums = nums.sort().
  • Shared Reference Trap: list2 = list1 does not clone a list; both point to the same memory object. Always use list2 = list1.copy().

Quick Summary

  • Adding: .append(item) (adds to end), .insert(index, item) (adds at position), .extend(list) (merges collections).
  • Removing: .pop(index) (removes and returns item), .remove(value) (removes first matching value), .clear() (empties list).
  • Inspection: len() (total items), .count(val) (occurrences), .index(val) (position lookup).
  • Sorting: .sort() modifies in place; sorted(list) returns a new sorted list without modifying the original.
  • Duplicating: Always use .copy() or slicing [:] to create independent clones.

What's Next?

Let's learn how to iterate and loop through list items using for loops, while loops, and enumerate() in the next lesson!