List Methods
Python lists come with powerful built-in methods that let you manipulate data with ease. Whether you need to add items, remove duplicates, sort data, or find elements, there's a method for that. Mastering these methods is essential for writing clean, efficient Python code!
Adding Elements
Three methods let you add elements to a list: append() adds one item to the end,
insert() adds at a specific position, and extend() adds multiple items
from another iterable.
Click Run to execute your code
append() adds its argument as a single
element, even if it's a list. extend() iterates over its argument and adds
each element individually. Use append() for single items, extend()
for combining lists.
Removing Elements
Python provides several ways to remove elements: remove() by value,
pop() by index (with return), del by index or slice, and
clear() to empty the entire list.
Click Run to execute your code
pop() when you need the removed value (like
implementing a stack). Use remove() when you know the value but not the index.
Use del when removing multiple items with a slice.
Sorting and Ordering
Sort lists with sort() (modifies in place) or sorted() (returns new list).
Reverse with reverse() or reversed(). Both support custom sorting with
the key parameter.
Click Run to execute your code
sort() and reverse() modify
the original list and return None. Don't do x = mylist.sort() -
x will be None! Use sorted() or reversed() if you need to keep
the original list unchanged.
Searching and Counting
Find elements with index(), count occurrences with count(),
and check membership with the in operator.
Click Run to execute your code
Common Mistakes
1. Assigning result of sort() or reverse()
# Wrong - sort() returns None!
numbers = [3, 1, 2]
sorted_nums = numbers.sort()
print(sorted_nums) # None!
# Correct
numbers.sort() # Modifies in place
print(numbers) # [1, 2, 3]
# Or use sorted() for new list
sorted_nums = sorted([3, 1, 2])
2. remove() raises error if item not found
# Wrong - crashes if item doesn't exist
fruits = ["apple", "banana"]
fruits.remove("grape") # ValueError!
# Correct - check first
if "grape" in fruits:
fruits.remove("grape")
else:
print("Not found")
3. Confusing append() with extend()
# Unexpected result
list1 = [1, 2, 3]
list1.append([4, 5])
print(list1) # [1, 2, 3, [4, 5]] - nested list!
# What you probably wanted
list2 = [1, 2, 3]
list2.extend([4, 5])
print(list2) # [1, 2, 3, 4, 5]
4. Modifying list while iterating
# Wrong - removing while iterating causes issues
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num % 2 == 0:
numbers.remove(num) # Skips elements!
# Correct - iterate over copy or use list comprehension
numbers = [1, 2, 3, 4, 5]
numbers = [num for num in numbers if num % 2 != 0]
print(numbers) # [1, 3, 5]
Exercise: List Operations
Task: Perform various operations on a list of numbers.
Requirements:
- Add the number 10 to the end of the list
- Remove the first occurrence of 5
- Sort the list in ascending order
- Print the final list
Click Run to execute your code
Show Solution
numbers = [5, 2, 9, 1, 5, 6]
# Add 10 to end
numbers.append(10)
# Remove first 5
numbers.remove(5)
# Sort ascending
numbers.sort()
print(f"Final list: {numbers}")
# Output: [1, 2, 5, 6, 9, 10]
Summary
- Adding:
append(x),insert(i, x),extend(iterable) - Removing:
remove(x),pop([i]),del list[i],clear() - Sorting:
sort()(in-place),sorted()(new list) - Reversing:
reverse()(in-place),reversed()(iterator) - Searching:
index(x),count(x),x in list - In-place methods: Return
None, modify original - Functional versions:
sorted(),reversed()preserve original
What's Next?
Now that you've mastered list methods, let's learn List Comprehensions - Python's elegant, one-liner syntax for creating and transforming lists. It's one of Python's most loved features!
Enjoying these tutorials?