Python Lists: Create, Update, Filter and Loop
Store an ordered collection, add and remove items, and build new lists from old ones.
- Creating lists and indexing them
- append, insert, remove and pop
- Looping, filtering and sorting
An ordered, changeable collection
Lists are ordered, mutable collections that can contain duplicate values and different data types.
Key points
- Lists keep their order and can hold duplicates and mixed types.
- append adds to the end; pop removes and returns the last item.
- Indexes start at 0, and negative indexes count from the end.
- Lists are mutable: changes are visible through every variable that refers to the list.
When should you use it?
- Learn Python syntax through a practical example.
- Build a foundation for larger programming problems.
- Test an idea quickly without a local setup.
See how it works
Use the buttons to change the list and watch the indices update. Move start and stop to see which items a slice takes.
Example code
numbers = [1, 2, 3, 4, 5]
numbers.append(6)
even_numbers = [number for number in numbers if number % 2 == 0]
print(even_numbers)
Result of running the code
[2, 4, 6]
Code explanation
The example appends a value and then uses a list comprehension to select only even numbers.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Python list methods and operations
nums = [3, 1, 2] | Create a list |
nums[0], nums[-1] | First and last item |
nums[1:3] | Slice: the items at index 1 and 2 |
nums.append(4) | Add one item to the end |
nums.insert(0, 9) | Insert an item at a position |
nums.extend([5, 6]) | Add every item from another list |
nums.remove(3) | Remove the first item equal to 3 |
nums.pop(), nums.pop(0) | Remove and return the last item, or the item at an index |
len(nums) | Number of items |
3 in nums | True if the list contains 3 |
nums.index(2), nums.count(2) | Position of the first 2, and how many 2s there are |
nums.sort(), sorted(nums) | Sort in place, or return a new sorted list |
Common mistakes with Python lists
Removing items while looping over the same list
Removing shifts the remaining items left, so the loop skips the item right after each removal.
nums = [1, 2, 2, 3]
for n in nums:
if n == 2:
nums.remove(n)
print(nums) # [1, 2, 3]: one 2 is left
nums = [1, 2, 2, 3]
nums = [n for n in nums if n != 2]
print(nums) # [1, 3]
Expecting sort() to return the sorted list
list.sort() sorts in place and returns None. Use sorted() when you need a new list.
result = [3, 1, 2].sort()
print(result) # None
result = sorted([3, 1, 2])
print(result) # [1, 2, 3]
Copying a list with =
b = a makes a second name for the same list, so changing one changes both.
a = [1, 2]
b = a
b.append(3)
print(a) # [1, 2, 3]
a = [1, 2]
b = a.copy()
b.append(3)
print(a) # [1, 2]
Reading one index past the end
Indexes run from 0 to len(nums) - 1, so nums[len(nums)] raises IndexError. Use nums[-1] for the last item.
nums = [10, 20, 30]
print(nums[len(nums)]) # IndexError
nums = [10, 20, 30]
print(nums[-1]) # 30
Frequently asked questions
What is the difference between a list and a tuple in Python?
A list can change after it is created: you can add, remove and replace items. A tuple cannot change, which makes it a good fit for fixed records and lets you use it as a dictionary key.
How do I add items to a list in Python?
Use append() to add one item to the end, extend() to add every item from another list, and insert() to put an item at a specific position.
How do I remove an item from a list?
Use remove(value) to delete the first matching value, pop(index) to remove and return the item at an index, or del nums[index] to delete it without returning it.
How do I sort a list in Python?
Call nums.sort() to sort the list itself, or sorted(nums) to get a new sorted list. Pass reverse=True for descending order and key= to sort by something else, such as key=len.
Can a Python list hold different types?
Yes. A list can mix numbers, strings, other lists and any other objects, although lists of one type are easier to work with.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Sum an Array Add up all values in a list. Easy
- Array Maximum Find the largest value in a list. Easy
- Running Sum Replace each value with the total so far. Easy
- Count Positive Numbers Count the values greater than zero in a list. Easy