Unpacking Lists and Tuples in Python

Unpack multiple values, ignore some, handle errors, and more

Little reindeer figures on the floor
Photo by Markus Spiske on Unsplash.

Packing means collecting several values into a single variable (tuple), like 
a=’red’,’square’,’apple’. Unpacking means extracting those values into different variables, like color,shape,fruit = a.

This is one way to do the unpacking. There are different ways to do it. Unpacking can be done on iterables like lists, tuples, ranges, and dictionaries.

In this article, I will cover the various ways to unpack Python lists and tuples.


Packing, Unpacking Python Tuples

  • Packing — Assigning multiple values to one tuple.
  • Unpacking — Assigning values from the tuple to many variables. This is also known as multiple assignment.

Unpacking can be done on sequences (lists, tuples, ranges).

Packing and unpacking Python tuples

https://gist.github.com/IndhumathyChelliah/05bfae887a234109802e557a18cd7e78

While packing many values to a tuple, parentheses are optional. While unpacking, I mentioned the number of variables equal to the length of the tuple.


Unpacking Python List, Range Object

Unpacking the elements of one Python list

https://gist.github.com/IndhumathyChelliah/180cc8845df668af34c49430d32eafe4

While unpacking the Python list, I mentioned the number of variables equal to the length of the list.

Unpacking two Python lists to a single tuple/list

We can unpack two Python lists into a single variable. It will return a tuple containing all the elements from both lists.

https://gist.github.com/IndhumathyChelliah/682fa3b4101b29ce57b5a44607ef9b8e

Unpacking two Python lists to a single list

We can unpack two Python lists into a variable. It will return a tuple containing all the elements from both lists. We can convert them to a list using the list() constructor.

This is one way to merge two lists:

https://gist.github.com/IndhumathyChelliah/d35bd6c56c69082c041ff90865086d22

Unpacking a range object

https://gist.github.com/IndhumathyChelliah/80384f43aeb2335bedf6d17c0515f499


Extended Iterable Unpacking

While unpacking, we have to mention the number of variables equal to the length of the sequence (list/tuple).

To overcome this, we can use extended iterable unpacking.

Extended unpacking uses the * operator. A variable preceded by the * operator is used for extended unpacking. It will be a list containing all elements from the iterable that is not assigned to variable names.

  • first,*rest — It will unpack the first element to first and all the remaining elements to rest. rest will be a list of all items except the first element.
  • first,*middle, last — It will unpack the first element to first and the last element to last. The remaining elements will be a list of all items except the first and last elements.
  • *rest, last — It will unpack the last element in last and all the remaining elements to rest. rest will be a list of all items except the last element.

Extended unpacking (tuples)

https://gist.github.com/IndhumathyChelliah/fa51a860a56619c1bad51fb7b4ca3675

Extended unpacking (lists)

https://gist.github.com/IndhumathyChelliah/fa51a860a56619c1bad51fb7b4ca3675

Unpacking first_element vs. rest

https://gist.github.com/IndhumathyChelliah/11520df679bcc9299b75e6e49013a2d5

Unpacking first, last, middle elements

https://gist.github.com/IndhumathyChelliah/2d69fdc0d75cee7c725a47055ad34e1b

Unpacking the last element vs. rest

https://gist.github.com/IndhumathyChelliah/a8137f751c087f5f2092a778b0d8d4b1


Ignoring Single Value While Unpacking Tuples/Lists

If we want to ignore a single value while unpacking, we can mention _.

Ignoring single value while unpacking

https://gist.github.com/IndhumathyChelliah/a8c7613632008ace6fdc8847a53376f4

Ignoring Multiple Values While Unpacking Tuples/Lists

If we need to ignore multiple values while unpacking, we can mention *_.

Ignoring all the values except the first and the last while unpacking

https://gist.github.com/IndhumathyChelliah/839ed2da4848e3e2400c631b1fceefed


Unpacking List of Tuples

https://gist.github.com/IndhumathyChelliah/1b4f13535bb00510ff05ed5fb6fac1b1


Errors While Unpacking

1. ValueError: Too many values to unpack

While unpacking tuples, if we give a lower number of variables when compared to the length of the tuple, it will throw an error.

https://gist.github.com/IndhumathyChelliah/b2ab113e0eb650f0edb667c6ca208202

2. ValueError: Not enough values to unpack

While unpacking tuples, if we give a higher number of variables when compared to the length of the tuple, it will throw an error.

https://gist.github.com/IndhumathyChelliah/96060f143761c1eb81b9ad067c118ecf


Unpacking Python Tuples

If our user-defined function returns a tuple, we can unpack those values using tuple unpacking.

Example

The user-defined calc() function returns a tuple containing the result of arithmetic operations (Addition, Subtraction, Multiplication, and Division).

We can unpack those values by using tuple unpacking:

https://gist.github.com/IndhumathyChelliah/55f447d5ea28df38553a33bf5f251512


Conclusion

In this article, I have covered different ways to unpack Python lists and tuples (e.g. how to unpack all the values into different variables, how to ignore some values while unpacking, etc.).

Thanks for reading!


Watch this space for more articles on Python and DataScience. If you like to read more of my tutorials, follow me on Medium, LinkedIn, Twitter.

 

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

Buy Me a CoffeeBuy Me a CoffeeBuy Me a Coffee

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s