Unpacking iterables into iterables PREMIUM

Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
3 min. read Watch as video Python 3.10—3.14
Python Morsels
Watch as video
02:35

Let's talk about merging together different iterables into a new list.

Concatenating iterables using the + operator

If you have a couple of lists in Python, you can use the + operator to concatenate those lists (to make a new list that has all the values in the first and the second list).

Here we're concatenating two lists called numbers and more_numbers into a new list:

>>> numbers = [2, 1, 3, 4]
>>> more_numbers = [7, 11, 18]
>>> numbers + more_numbers
[2, 1, 3, 4, 7, 11, 18]

We can also use the + operator to concatenate tuples:

>>> p = (1, 2)
>>> q = (3, 4)
>>> p + q
(1, 2, 3, 4)

But what if we wanted to concatenate different iterables into a new list?

For example, here we have some lists and some tuples that we want to concatenate:

>>> numbers = [2, 1, 3, 4]
>>> more_numbers = [7, 11, 18]
>>> p = (1, 2)
>>> q = (3, 4)

Will the + operator work to concatenate lists and tuples together?

>>> numbers + more_numbers + p + q

It won't.

>>> numbers + more_numbers + p + q
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list

In Python, you can't concatenate different types of objects.

We could instead take all of our iterables and convert them to lists (assuming we don't know that they're already lists) and then concatenate them into a new list:

>>> list(numbers) + list(more_numbers) + list(p) + list(q)
[2, 1, 3, 4, 7, 11, 18, 1, 2, 3, 4]

But there's an even better way to do this.

Using the * operator to concatenate different types of iterables

In Python, we can take lots of different iterables and make a new list out of them using the * operator:

>>> [*numbers, *more_numbers, *p, *q]
[2, 1, 3, 4, 7, 11, 18, 1, 2, 3, 4]

You usually see the * operator used in function calls. But you can also use it in the list literal syntax (inside of square brackets that are used to build up a new list).

The * operator also works with the tuple literal syntax:

>>> (*numbers, *more_numbers, *p, *q)
(2, 1, 3, 4, 7, 11, 18, 1, 2, 3, 4)

And the set literal syntax:

>>> {*numbers, *more_numbers, *p, *q}
{1, 2, 3, 4, 7, 11, 18}

This syntax doesn't just work with *. This is actually the same exact syntax that's used for building up a new list. That means we can mix and match unpacking iterables and putting individual items into our new list.

For example, here we're making a list that starts with 0, then unpacks the squares iterable, unpacks the numbers iterable, and then ends with 100:

>>> numbers = [2, 1, 3, 4]
>>> squares = (n**2 for n in numbers)
>>> [0, *squares, *numbers, 100]
[0, 4, 1, 9, 16, 2, 1, 3, 4, 100]

Also note that squares above is a generator object. The * operator works with any iterable; not just with lists, tuples, and sets.

When shouldn't you use the * operator?

The * operator can sometimes make your code a little bit harder to read. There is one place I definitely recommend against using the * operator.

If you trying to copy an iterable into a new list, you could use * to do that:

>>> colors = {'purple', 'blue', 'green'}
>>> colors_list = [*colors]
>>> colors_list
['purple', 'blue', 'green']

But there's a better way to do this in Python. The list constructor (the built-in list function) is specifically meant for looping an iterable and turning it into a list:

>>> colors = {'purple', 'blue', 'green'}
>>> colors_list = list(colors)

I read this line as "unpack an iterable into a new list":

colors_list = [*colors]

But I read this line as "turn an iterable into a list":

colors_list = list(colors)

Most Python programmers will find that list constructor easier to read than that use of the * operator.

Summary

If you have multiple iterables that you'd like to merge together into a new list, you can use the * operator to unpack those iterables into that new list.

Now it's your turn! 🚀

We don't learn by reading or watching. We learn by doing. That means writing Python code.

Practice this topic by working on these related Python exercises.

Python Morsels
Watch as video
02:35
This is a free preview of a premium screencast. You have 1 preview remaining.