One can unpack an N-element tuple or iterable sequence into a collection of n-variables by using the following pattern. It's important to note that the number of variables and elements in the sequence need to match. Below is a list with a string, int, float, and a tuple. Variables are paired with data in the same order from left to right.

data = ['Carolin', 50, 98.7, (2017, 3, 20)]
name, age, temp, date = data


This doesn't only apply to tuples and lists; it can be any iterable object. In this case, we have a string and each letter is assigned to a letter variable.

s = 'Test'
a, b, c, d = s
print(a)


We can see that printing the variable a produces the first letter in our string.

T

If you you have more items in your sequence than you do variables, you'll get a warning letting you know that you have too many. Here, the traceback message is pretty clear.

data = ['Carolin', 50, 98.7, (2017, 3, 20)]
name, age, temp = data


ValueError Traceback (most recent call last)

<ipython-input-2-b1385ed3051e> in () 1 data = ['Carolin', 50, 98.7, (2017, 3, 20)] ----> 2 name, age, temp = data

ValueError: too many values to unpack (expected 3)

On the other hand, if you have too many variables, but not enough items in your sequence, the traceback will let you know that it expected more from the sequence, but only got x number.

data = ['Carolin', 50, 98.7, (2017, 3, 20)]
name, age, temp, date, time = data


ValueError Traceback (most recent call last)

<ipython-input-3-3c6a6bafdea4> in () 1 data = ['Carolin', 50, 98.7, (2017, 3, 20)] ----> 2 name, age, temp, date, time = data

ValueError: not enough values to unpack (expected 5, got 4)

You can even unpack a sequence inside of a sequence. In this case, you simply need to assign variables to each piece of data in the tuple.

data = ['Carolin', 50, 98.7, (2017, 3, 20)]
name, age, temp, (year, mon, day) = data
print('Name: ', name)
print('Year: ', year)
print('Month: ', mon)
print('Day: ', day)


Name: Carolin

Year: 2017

Month: 3

Day: 20

If you want to delete some of the valuables, you can get rid of them by assigning them to a throwaway variable.

data = ['Carolin', 50, 98.7, (2017, 3, 20)]
_, age, temp, _ = data
print('Age: ', age)
print('Temperature: ', temp)


Age: 50

Temperature: 98.7

What if you don't know how long the list is? You can add an asterix. In this case, Python is able to assign the first and last variables automatically, and fills the middle variables by using the asterix notation. Here, you can add or remove as many numbers as you like and the function will drop the first and last and find the mean of the remaining numbers in the middle.

from statistics import mean

grades = (4, 6, 5, 2, 5, 9, 5, 8, 9, 6, 7, 5)

return mean(middle)



Mean: 6.2

Here is an example. I deleted a few of the numbers from the middle and the function still works just fine.

from statistics import mean

grades = (4, 6, 5, 2, 5, 9, 6, 7, 5)

return mean(middle)



Mean: 5.714285714285714

You can also apply this elsewhere, where you're not sure how many of something you'll have. In this context, you may want to store multiple phone numbers. In this example, the first varialbe is associated with a name, the second with an email, and every entry afterwards will be associated with the phone_numbers variable.

contacts = ('Charlotte', 'choconana@test.com', '01515251112',
'0165492255589')

name, email, *phone_numbers = contacts
cell, home = phone_numbers
print('Name: ', name)
print('Email: ', email)
print('Phone Numbers: ', phone_numbers)
print('Cell phone: ', cell)
print('Home phone: ', home)


You can see the phone_numbers variable will return a list unless you use the pattern to match the contents. If you don't have consistent or clean data, you may end up with a traceback if you try unpacking right away.

Name: Charlotte

Email: choconana@test.com

Phone Numbers: ['01515251112', '0165492255589']

Cell phone: 01515251112

Home phone: 0165492255589

You can also use star notation to unpack and throw away variables like we did before.

data = ['Carolin', 50, 98.7, (2017, 3, 20)]
name, *_, (year, *_) = data
print('Name: ', name)
print('Date: ', year)


Name: Carolin

Date: 2017