We all know pretty much that Python provides a variety of well-known built-in data structures, including lists, sets, and so on. Unpacking a sequence into separate variables is a simple task if you use Python for it as well.
Suppose we have a tuple or a sequence of N elements like below:
mySequence = [ 'FBA', 20, 3000.30, (18, 12, 2000) ]
I want to unpack all of the sequential data into different variables, but I want to do that using a simple assignment operation. I can do that using a simple assignment operation in Python. Let me show you how.
a, b, c, d = mySequence
Now, all the sequential data have been assigned to my newly provided variables successfully. Let's check that out.
print(a)
print(b)
print(c)
print(d)
The full code would be like this:
mySequence = [ 'FBA', 20, 3000.30, (18, 12, 2000) ]
a, b, c, d = mySequence
print(a)
print(b)
print(c)
print(d)
The output would be:
FBA
20
3000.3
(18, 12, 2000)
We can provide meaningful variable names for our needs as well. You can check the following code also.
mySequence = [ 'FBA', 20, 3000.30, (18, 12, 2000) ]
name, age, salary, dateOfBirth = mySequence
print(name)
print(age)
print(salary)
print(dateOfBirth)
The output is given below:
FBA
20
3000.3
(18, 12, 2000)
We can get the day, month, and year from the dateOfBirth
variable as well by unpacking the variable.
mySequence = [ 'FBA', 20, 3000.30, (18, 12, 2000) ]
name, age, salary, (day, month, year) = mySequence
print(name)
print(age)
print(salary)
print(day)
print(month)
print(year)
The output is given below.
FBA
20
3000.3
18
12
2000
This unpacking technique will work with any object that is iterable, not just simply lists or tuples. You can use this in strings, iterators, generators, and files as well.
myString = 'United'
p, q, r, s, t, u = myString
print(r)
#Output: i
Keep in mind that if there happens any mismatch in the number of elements, then we will get an error. An example would be as follows.
x = (1, 2, 3)
p, q = x
As there are three numbers in the sequence, but we wanted to assign them to two variables instead of three variables, we will receive an error as follows.
Traceback (most recent call last):
File "/home/fba/Desktop/Practice/1.py", line 2, in <module>
p, q = x
ValueError: too many values to unpack (expected 2)
During this unpacking, we might want to discard certain values. Sadly, python does not have any special syntax for this. We can just pick a throwaway variable name for that. An example would be as follows.
myString = ['FBA' , 'IJK' , 90, 11]
_, a, b, _ = myString
Here, we are using _
variable as a throwaway variable. Remember, if you use the same throwable variable more than one time, then if you want to print that throwable variable later, you will get the latest value you have assigned to the variable earlier in the output. Like, in the code given above, we have used _
variable both for the data FBA
and the data 11
. If you want to print the value of _
variable, then we will get 11
as the output as the latest value we have assigned to the _
variable was 11
.
myString = ['FBA' , 'IJK' , 90, 11]
_, a, b, _ = myString
print(_)
The output is as follows.
11
Thanks for reading this entire article. If you want, you may follow me on Twitter, LinkedIn, GitHub. Here is my website as well. Have a great day!
Top comments (0)