How to Pack and Unpack a Tuple in Python? (+ ValueError) - Python Tutorial for Beginners

preview_player
Показать описание
🎓 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


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

- Digital Academy™ 🎓

***

☞ WATCH NEXT:

#Python #Tutorial #Beginners #Shorts

***

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

***

♡ FOLLOW US ♡

♡ SUPPORT US ♡

***
Рекомендации по теме
Комментарии
Автор

How tu Pack and Unpack a Tuple in Python, with common Errors? 🤔

DigitalAcademyOnline
Автор

I am struck with this error,
X, y, z =input("expression:").split()
New_x=float(×)
New_z=float(z)
Here y do is arithmetic operation example
X=1 y is a(×, +, -, *, /)z is 1
I am getting a error not enough value to unpack expected 3 got 1

saimurugeshwari
Автор

Can you speak urdu please i cant understand english

YousufKhan-vedk