DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,864 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Unpacking a Sequence into Separate Variables using Python
Md. Fahim Bin Amin
Md. Fahim Bin Amin

Posted on

Unpacking a Sequence into Separate Variables using Python

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 an tuple or a sequence of N elements like below:

mySequence = [ 'FBA' , 20, 3000.30, (18, 12, 2000) ]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Now, all the sequential data have been assigned to my newly provided variables successfully. Lets check that out.

print(a)
print(b)
print(c)
print(d)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

The output would be:

FBA
20
3000.3
(18, 12, 2000)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

The output is given below:

FBA
20
3000.3
(18, 12, 2000)
Enter fullscreen mode Exit fullscreen mode

We can get the day, month and the 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)
Enter fullscreen mode Exit fullscreen mode

The output is given below.

FBA
20
3000.3
18
12
2000
Enter fullscreen mode Exit fullscreen mode

VS Code preview

Terminal Preview

This unpacking technique will work with any object that is iterable, not just simply list or tuples. You can use this in strings, iterators, generators, files as well.

myString = 'United'
p, q, r, s, t, u = myString
print(r)

#Output: i
Enter fullscreen mode Exit fullscreen mode

Keep in mind that if there happens any mismatch in the number of the elements, then we will get an error. An example would be as follows.

x = (1, 2, 3)
p, q = x
Enter fullscreen mode Exit fullscreen mode

As there are three numbers in the sequence, but we wanted to assign them into 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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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(_)
Enter fullscreen mode Exit fullscreen mode

The output is as follows.

11
Enter fullscreen mode Exit fullscreen mode

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)

🌚 Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.