# What Is Tuple Unpacking In Python

Destructuring operation unpacking data structures into named references

To decrypt the definition, look for types and examples below. If in doubt, reach out to Multiple assignment and tuple unpacking improve Python code readability — Trey Hunner

## 1. LITERAL

• aka multiple assignment
• this is not only with a simple binding of literal values in a function that calculates the running total of natural numbers from a → b
• `total, k = 0, 1` unpacks the ordered pair `(0,1)` into `total = 0` and `k = 1`
`def sum_naturals(n):        total, k = 0, 1 #SEE ABOVE        while k <= n:            total, k = total + k, k + 1        return total`
`>>> pair = (1,2)>>> pair(1,2)>>> x,y = pair>>> x1>>> y 2`

## 2. FUNCTIONAL

• say that you have a function to get the min and max values of a tuple
`def minmax(items):    return min(items), max(items)min, max = minmax(items)# ALL PASSEDdef test_minmax_tupple():    input = 1, 2, 3, 4, 5    result = minmax(input)    assert result == (1, 5)def test_minmax_min():    input = 1, 5, 7    resultMin = minmax(input)    assert resultMin == 1def test_minmax_max():    input = 1, 33, 99    resultMax = minmax(input)    assert resultMax == 99def test_minmax_min_unpacked():    input = 1, 5, 7    resultMin, resultMax = minmax(input)    assert resultMin == 1def test_minmax_max_unpacked():    input = 1, 33, 99    resultMin, resultMax = minmax(input)    assert resultMax == 99`

## 4. SWAPPING

• assign bindings
`a = 'Hello'b = 'World'`
`a,b = b,a`
1. unpacks the new tuple on the left — it reuses the names `a` and `b`
`>>>a'World'>>>b'Hello'`

## 5. SOURCES

--

-- ## Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com