Python Basics

Python Lists: Create, Update, Filter and Loop

Store an ordered collection, add and remove items, and build new lists from old ones.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Creating lists and indexing them
  • append, insert, remove and pop
  • Looping, filtering and sorting
nums = [1, 2, 3, 4, 5, 6]
1[0]
2[1]
3[2]
4[3]
5[4]
6[5]
Keeping the even values gives [2, 4, 6].

An ordered, changeable collection

Lists are ordered, mutable collections that can contain duplicate values and different data types.

Key points

  1. Lists keep their order and can hold duplicates and mixed types.
  2. append adds to the end; pop removes and returns the last item.
  3. Indexes start at 0, and negative indexes count from the end.
  4. 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

Run code →
main.py
numbers = [1, 2, 3, 4, 5]
numbers.append(6)

even_numbers = [number for number in numbers if number % 2 == 0]
print(even_numbers)
Get a Run button for this example

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.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

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 numsTrue 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.

Problem
nums = [1, 2, 2, 3]
for n in nums:
    if n == 2:
        nums.remove(n)
print(nums)  # [1, 2, 3]: one 2 is left
Fix
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.

Problem
result = [3, 1, 2].sort()
print(result)  # None
Fix
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.

Problem
a = [1, 2]
b = a
b.append(3)
print(a)  # [1, 2, 3]
Fix
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.

Problem
nums = [10, 20, 30]
print(nums[len(nums)])  # IndexError
Fix
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.

All challenges