Unpack tuple with one element python

The prettiest way to get the first element and the last element of an iterable object like tuple, list, etc. would be to use the * feature not same as the * operator.

my_tup = ('a', 'b', 'c',)

# Last element
*other_els, last_el = my_tup

# First element
first_el, *other_els = my_tup

# You can always do index slicing similar to lists, eg [:-1], [-1] and [0], [1:]

# Cool part is since * is not greedy (meaning zero or infinite matches work) similar to regex's *. 
# This will result in no exceptions if you have only 1 element in the tuple.
my_tup = ('a',)

# This still works
# Last element
*other_els, last_el = my_tup

# First element
first_el, *other_els = my_tup

# other_els is [] here

Summary: in this tutorial, you’ll learn how to unpack tuples in Python.

Reviewing Python tuples

Python defines a tuple using commas (,), not parentheses (). For example, the following defines a tuple with two elements:

1,2

Code language: Python (python)

Python uses the parentheses to make the tuple clearer:

(1, 2)

Code language: Python (python)

Python also uses the parentheses to create an empty tuple:

()

Code language: Python (python)

In addition, you can use the tuple() constructor like this:

tuple()

Code language: Python (python)

To define a tuple with only one element, you still need to use a comma. The following example illustrates how to define a tuple with one element:

1,

Code language: Python (python)

It’s equivalent to the following:

(1, )

Code language: Python (python)

Note that the following is an integer, not a tuple:

(1)

Code language: Python (python)

Unpacking a tuple

Unpacking a tuple means splitting the tuple’s elements into individual variables. For example:

x, y = (1, 2)

Code language: Python (python)

The left side:

x, y

Code language: Python (python)

is a tuple of two variables x and y.

The right side is also a tuple of two integers 1 and 2.

The expression assigns the tuple elements on the right side (1, 2) to each variable on the left side (x, y) based on the relative position of each element.

In the above example, x will take 1 and y will take 2.

See another example:

x, y ,z = 10, 20, 30

Code language: Python (python)

The right side is a tuple of three integers 10, 20, and 30. You can quickly check its type as follows:

numbers = 10, 20, 30 print(type(numbers))

Code language: Python (python)

Output:

<class 'tuple'>

Code language: Python (python)

In the above example, the x, y, and z variables will take the values 10, 20, and 30 respectively.

Using unpacking tuple to swap values of two variables

Traditionally, to swap the values of two variables, you would use a temporary variable like this:

x = 10 y = 20 print(f'x={x}, y={y}') tmp = x x = y y = tmp print(f'x={x}, y={y}')

Code language: Python (python)

Output:

x=10, y=20 x=20, y=10

Code language: Python (python)

In Python, you can use the unpacking tuple syntax to achieve the same result:

x = 10 y = 20 print(f'x={x}, y={y}') x, y = y, x print(f'x={x}, y={y}')

Code language: Python (python)

Output:

x=10, y=20 x=20, y=10

Code language: Python (python)

The following expression swaps the values of two variables, x and y.

x, y = y, x

Code language: Python (python)

In this expression, Python evaluates the right-hand side first and then assigns the variable from the left-hand side to the values from the right-hand side.

ValueError: too many values to unpack

The following example unpacks the elements of a tuple into variables. However, it’ll result in an error:

x, y = 10, 20, 30

Code language: Python (python)

Error:

ValueError: too many values to unpack (expected 2)

Code language: Python (python)

This error is because the right-hand side returns three values while the left-hand side only has two variables.

To fix this, you can add a _ variable:

x, y, _ = 10, 20, 30

Code language: Python (python)

The _ variable is a regular variable in Python. By convention, it’s called a dummy variable.

Typically, you use the dummy variable to unpack when you don’t care and use its value afterward.

Extended unpacking using the * operator

Sometimes, you don’t want to unpack every single item in a tuple. For example, you may want to unpack the first and second elements. In this case, you can use the * operator. For example:

r, g, *other = (192, 210, 100, 0.5)

Code language: Python (python)

Output:

192 210 [100, 0.5]

Code language: Python (python)

In this example, Python assigns 192 to r, 210 to g. Also, Python packs the remaining elements 100 and 0.5 into a list and assigns it to the other variable.

Notice that you can only use the * operator once on the left-hand side of an unpacking assignment.

The following example results in error:

x, y, *z, *t = (10, 20, 30, '10:30')

Code language: Python (python)

Error:

SyntaxError: two starred expressions in assignment

Code language: Python (python)

Using the * operator on the right hand side

Python allows you to use the * operator on the right-hand side. Suppose that you have two tuples:

odd_numbers = (1, 3, 5) even_numbers = (2, 4, 6)

Code language: Python (python)

The following example uses the * operator to unpack those tuples and merge them into a single tuple:

numbers = (*odd_numbers, *even_numbers) print(numbers)

Code language: Python (python)

Output:

(1, 3, 5, 2, 4, 6)

Code language: Python (python)

Summary

  • Python uses the commas (,) to define a tuple, not parentheses.
  • Unpacking tuples means assigning individual elements of a tuple to multiple variables.
  • Use the * operator to assign remaining elements of an unpacking assignment into a list and assign it to a variable.

Did you find this tutorial helpful ?

Can you unpack a tuple in Python?

In python tuples can be unpacked using a function in function tuple is passed and in function values are unpacked into normal variable.

How do you get one element of a tuple?

Use indexing to get the first element of each tuple Use a for-loop to iterate though a list of tuples. Within the for-loop, use the indexing syntax tuple[0] to access the first element of each tuple , and call list. append(object) with object as the tuple's first element to append each first element to list .

How do you extract an element from a tuple in Python?

To extract the n-th elements from a list of tuples with Python, we can use list comprehension. We get the n-th element from each tuple and put them into a list with [x[n] for x in elements] . x is the tuple being retrieved. Therefore, e is [1, 3, 5] .

Can you have a tuple with one element Python?

To create a tuple with only one item, you have add a comma after the item, otherwise Python will not recognize the variable as a tuple.