Python – Unpacking Iterables

By | February 9, 2018

Python iterables are lists, tuples, dictionaries and other similar objects – in other words things that can be iterated over. Python provides a way to ‘unpack’ these iterables which can be a useful shortcut in some situations. This post looks at unpacking iterables, and how it can also be used to make functions functions take a variable number of arguments.

Unpacking Iterables

An iterator in python is an object like a list or a tuple – in other words, things you can iterate over. There are times when you you want to separate out the elements in an iterator. For example a list may contain elements that you want to fit into a print statement:

x = ['one', 'two', 'three']
print("{0}, {1}, {2}".format(x[0], x[1], x[2]))

or there may be a function which takes certain values which you have stored in a list:

def func_test(one, two, three):
    print(one)
    print(two)
    print(three)

x = ['one', 'two', 'three']

func_test(x[0], x[1], x[2])

Accessing the individual elements in this way works up to a point, but it can be cumbersome under some circumstances such as where you don’t know how many items there are in your iterator, or there are many items and writing our x[i] many times is repetitive.

For these situations, python has a short-hand – the ‘*’ – to unpack an iterator of any length, without needing repetition.

Taking the two examples above again, we can use the * operator to unpack the iterator. In this first example *x ‘unpacks’ the elements of ‘x’ into the argument of the format function – it is almost like the list object becomes a series of comma-separated values.

x = ['one', 'two', 'three']

print("{0}, {1}, {2}".format(x[0], x[1], x[2]))
print("{0}, {1}, {2}".format(*x))

The effect of the * operator is similar in the function example. *x ‘unpacks’ the elements of the x list and places those into the argument of the func_test function.

def func_test(one, two, three):
    print(one)
    print(two)
    print(three)

x = ['one', 'two', 'three']

func_test(*x)

Unpacking Dictionaries

It is also possible to unpack dictionaries using a similar shortcut. For dictionaries, however we use the double asterisk: ‘**’ to unpack.

This can be useful you have a function with some known arguments you need to pass in. You can construct your argument list as a dictionary and pass that dictionary as an argument. This may be useful to you as you can then create the dictionary programmatically.

def func_test(one, two, three):
    print(one)
    print(two)
    print(three)

y = {'one': 1, 'two': 2, 'three': 3}
   
func_test(**y)