How to Pack and Unpack a Tuple in Python? (+ ValueError)

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ How to Pack a Tuple and Unpack a Tuple in Python?

○ Tuple Packing in Python

When a tuple is created, the items in the tuple are packed together into an object. In this example, the values ‘red’, ‘green’, ‘blue’ and ‘yellow’ are packed all together, into a tuple.

my_tuple = ('red', 'green', 'blue', 'yellow')
# my_tuple = ('red', 'green', 'blue', 'yellow')



○ Tuple Unpacking in Python

When a packed tuple is then unpacked, the individual items are unpacked and assigned to the items you defined. In the below example, the tuple “my_tuple” is unpacked into the variables ‘red’, ‘green’, ‘blue’ and ‘yellow’ so you can access each one of them individually.

my_tuple = ('red', 'green', 'blue', 'yellow')
(red, green, blue, yellow) = my_tuple

print(red) # red
print(green) # green
print(blue) # blue
print(yellow) # yellow


Nota: When unpacking, the number of variables on the left MUST match the number of items in the tuple.


○ Common Errors while Unpacking a Tuple in Python

Here are some very common errors during tuple unpacking – too many arguments, or not enough values to unpack!

my_tuple = ('red', 'green', 'blue', 'yellow')
(red, green) = my_tuple
# ValueError: too many values to unpack

my_tuple = ('red', 'green', 'blue')
(red, green, blue, extra) = my_tuple
# ValueError: not enough values to unpack (expected 4, got 3)



○ Unpack Tuple without Storing its Values (_)

Eventually, you may also want to unpack only some values from your tuple, but do not store all of its items inside each variable. Fortunately, Python offers this feature, using “_”. This way, each item is mapped into a new (placeholder) variable when it has one affected, and store its value so you can access it later.

# Do not store the value yellow
my_tuple = ('red', 'green', 'blue', 'yellow')
red, green, blue, _ = my_tuple






Awesome! But, you do not have any idea when using packing and unpacking methods might be useful for you? Here are a few examples in which you may want to pack and unpack a tuple.

Tuple unpacking comes handy when you want to swap values of two variables, without using a temporary variable.

a = 1
b = 99

# Swap values
a, b = b, a

print(a) # 99
print(b) # 1


While unpacking a tuple, the right side can be any kind of sequence: tuple, string or list. Typically, you may also apply any method on your variable that will return this kind of sequence, then proceed with unpacking. For instance, this example will split a string representing an email address into its user and domain, so you can access them.

# Split an email address
addr = 'digital.academy@free.fr'
user, domain = addr.split('@')

# user = digital.academy
# domain = free.fr



Let’s play this video, stick around and watch until the end of this video! 👍🏻

– Digital Academy™ 🎓

***

☞ WATCH NEXT:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho

☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw

📖 Blog: http://digital.academy.free.fr/blog/

📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners

🛒 Shopping and Discounts: http://digital.academy.free.fr/store/

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
http://digital.academy.free.fr/
https://twitter.com/DigitalAcademyy
https://www.facebook.com/digitalacademyfr
https://www.instagram.com/digital_academy_fr/
https://www.youtube.com/c/DigitalAcademyOnline

♡ SUPPORT US ♡
http://digital.academy.free.fr/join
http://digital.academy.free.fr/donate
http://digital.academy.free.fr/subscribe
https://www.patreon.com/digital_academy
https://www.buymeacoffee.com/digital_academy

***

Copy link
Powered by Social Snap