Exploring Python's Extended Unpacking

This feature that is not very well known within Python can help you manage your iterables in a clean and concise way.

Extended unpacking is, in my opinion, one of the most under-used built-in functionalities within Python.

It’s a powerful feature that allows developers to simplify code, especially when dealing with iterable objects like lists and tuples.

Table of Contents

What is Extended Unpacking?

Extended unpacking allows you to extract values from iterables in a clean and concise manner.

It’s particularly useful when you need to extract multiple elements from an iterable and still retain the remaining items in a separate variable.

Traditional Unpacking vs Extended Unpacking

Suppose that you have a list that you wanted to unpack. Traditionally, this would look something like this:

a, b, c = [0, 1, 2]

# a -> 0
# b -> 1
# c -> 2

While this is elegant for smaller lists, if you had a list of really anything larger, the code would be not-so-elegant:

a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z = list(range(26))

Let’s say that in this scenario, we don’t care about all of the values between b and y. We can use extended unpacking to clean up the code and get the variables we want:

a, *dont_care, z = list(range(26))

Here, we’re extracting the first and last element in the list and then placing everything else in the dont_care variable.

Similarly, we can do this with the first (and last) n variables:

# First 3 elements
one, two, three, *rest = list(range(100))

# Last 5 elements
*rest, five, four, three, two, one = list(range(100))

Practical Examples of Extended Unpacking

Extended unpacking is relevant in many scenarios where you deal with lists or tuples of variable lengths. Here are a few common scenarios:

  1. Splitting data: Useful for when you need to extract the first few elements from a list while keeping the remainder of the list intact.

  2. Swapping and Shuffling Elements: Manipulating lists can be easier given that what you’re trying to swap/shuffle is either at the front and/or rear of the list.

  3. Handling arbitrary-length iterables: In scenarios where you may be working with iterables of unknown length, extended unpacking allows you to manage these without having to worry about indexing.

📧 Join the Python Snacks Newsletter! 🐍

Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:

  1. Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.

  2. Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.

  3. Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.

You can unsubscribe at any time.

Interested in starting a newsletter or a blog?

Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.

If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.

This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.

Reply

or to participate.