Paul's Python CoursePPC

How do you add items to a list in Python?

, 2 minutes to read

To add one item to the end of a list, use append(), as in shopping.append("eggs"). To put an item somewhere other than the end, use insert() with a position, and to add several items from another list in one go, use extend().

append puts one item on the end

shopping = ["bread", "milk"]
shopping.append("eggs")
print(shopping)
['bread', 'milk', 'eggs']

You write append after the list's name with a dot, and it changes the list itself. There's no = on that line, and you don't need one. Adding one is a very common slip, in fact. shopping = shopping.append("eggs") leaves shopping holding None, because append does its work on the list and then hands back None, which the = stores, and the list itself is lost. Keep append on a line of its own.

A lot of programs start with an empty list, [], and append to it as they go, for example once for each pass of a loop or each thing the user types in.

insert and extend

insert() takes a position and an item. Positions count from 0, so insert(1, "eggs") puts eggs second, and everything after it moves along one place. extend() takes another list and adds each of its items to the end. Writing += with a list does the same job.

shopping = ["bread", "milk"]
shopping.insert(1, "eggs")
print(shopping)
shopping += ["jam", "tea"]
print(shopping)
['bread', 'eggs', 'milk']
['bread', 'eggs', 'milk', 'jam', 'tea']

The difference between append and extend is easy to trip over. If you append a list, the whole list goes in as a single item, so you end up with a list inside your list.

shopping = ["bread", "milk"]
shopping.append(["eggs", "jam"])
print(shopping)
print(len(shopping))
['bread', 'milk', ['eggs', 'jam']]
3

The length is 3 because the inner list counts as one item. If you wanted eggs and jam as two separate items, extend is the one to use.

Try it

Here's a short to-do list that uses all three.

Run it

main.py
tasks = ["water the plants"]
tasks.append("post the letter")
tasks.insert(0, "feed the cat")
tasks.extend(["ring the dentist", "buy bread"])
print(tasks)
print(len(tasks), "things to do")

Output

Press Run and what your program prints shows up here.

It prints the five tasks with feeding the cat first, since insert(0, ...) put it at the front. Try changing the 0 to 2 and running it again to see where the cat ends up. Then swap extend for append on line 4 and look at what happens to the count.

A small exercise

Start with an empty list called squares. Use a for loop and append() to fill it with the square numbers from 1 to 25, then print the list. It should show [1, 4, 9, 16, 25].

Show an answer
squares = []
for n in range(1, 6):
    squares.append(n * n)
print(squares)

The loop runs five times, with n going from 1 to 5, and each pass appends one more square. The print is outside the loop, so the list is shown once, after it's full.

You'll build a to-do list that adds tasks, numbers them and ticks them off in the To-Do List project. The Handbook page on append and pop covers taking items off again, and list methods has the rest.

The project that goes with it

To-Do List is a free project in Act 1, for Riya. It runs in your browser with nothing to install, and you don't need an account for it.

More guides