Extend

Author

Clayton Cafiero

Published

2025-06-09

The list method .extend()

Now that we’ve seen sequences and iterables, we can introduce a useful list method, .extend(). It’s often the case that we wish to append more than one element to an existing list.

First, let’s review the limitation of append, and look at some ways to make a bigger list from smaller elements.

Recall that the append method requires an argument—the thing that gets appended to the list.

>>> shopping_list = ['lettuce', 'hummus', 'lentils']
>>> shopping_list.append('lemon')
>>> shopping_list
['lettuce', 'hummus', 'lentils', 'lemon']

But what if we want to add several elements? We could use .append() in a loop, but we can’t do this:

>>> shopping_list = ['lettuce', 'hummus', 'lentils']
>>> shopping_list.append(['lemon', 'tahini', 'sumac'])

I mean, this is OK. It appends to shopping_list, but what does it append? Not the elements of the list ['lemon', 'tahini', 'sumac'], but the list itself! So the result is:

>>> shopping_list
['lettuce', 'hummus', 'lentils', ['lemon', 'tahini', 'sumac']]

That’s a list within a list. Not at all what we wanted.

We could concatenate the two lists, but that requires an assignment.

>>> shopping_list = ['lettuce', 'hummus', 'lentils']
shopping_list = shopping_list + ['lemon', 'tahini', 'sumac']
>>> shopping_list
['lettuce', 'hummus', 'lentils', 'lemon', 'tahini', 'sumac']

That’s slightly inelegant.

We could use a slice:

>>> shopping_list = ['lettuce', 'hummus', 'lentils']
>>> shopping_list[len(shopping_list):] = ['lemon', 'tahini', 'sumac']
>>> shopping_list
['lettuce', 'hummus', 'lentils', 'lemon', 'tahini', 'sumac']

That’s opaque at best—indeed, it’s almost obfuscatory!

Here’s how we’d do the same with .extend().

>>> shopping_list = ['lettuce', 'hummus', 'lentils']
>>> shopping_list.extend(['lemon', 'tahini', 'sumac'])
>>> shopping_list
['lettuce', 'hummus', 'lentils', 'lemon', 'tahini', 'sumac']

Much easier to read; much prettier.

.extend() takes an iterable as an argument and appends each of the elements of the iterable to the underlying list. (Do you think there might be a loop lurking in the background?)

However, we can’t dispense with .append(), because .extend() only takes an iterable as an argument. For example, this fails:

>>> numbers = [1, 2, 3]
>>> numbers.extend(4)
Traceback (most recent call last):
  File "<python-input-21>", line 1, in <module>
    numbers.extend(4)
    ~~~~~~~~~~~~~~^^^
TypeError: 'int' object is not iterable

This fails because integers aren’t iterable.

Comprehension check

  1. Consider this interaction at the shell:
>>> shopping_list = ['lettuce', 'hummus', 'lentils']
>>> shopping_list.extend('lemon')

This succeeds, but what is the resulting list?

  1. There’s a way to append a single element to a list with .extend(). Can you produce an example?

  2. What do you think happens here?

>>> numbers = [1, 2, 3]
>>> numbers.extend(range(4, 7))

Copyright © 2023–2025 Clayton Cafiero

No generative AI was used in producing this material. This was written the old-fashioned way.