Iterables Packing UnPacking In Python

Tuple By Sagar Jaybhay

A=1,2,3 

Is this tuple the answer is yes . The only parenthesis not defined tuples in python comma are defined this is tuple or not. But when we want to create an element with a single element is

A=(1,)
A=1,

This is a tuple with single value. value but when you create

A=(1) 

Is not a tuple. But when you want to create empty tuple then

A=() #this is allowed.
Tuple In Python

Packed Values:

In this packed value refers to values which are bundled in a single unit.

A=(1,2,3)
B=[1,2,3]
C=’Sagar’

All these are packed values. In string individual character bundled together. An any iterable is packed value.

UnPacked Values:

It means you get iterable and assign the value in that with a variable in python.

a, b, c = (1, 2, 3)
print('a :', a, ' b :', b, ' c:', c)

a, b, c = 'xyz'
print('a :', a, ' b :', b, ' c:', c)

for an in 'sagar jaybhay':
    print(a)

you can iterate through dictionaries

dic = {'key1': 2, 'key2': 2, 'key3': 3}
for e in dic:
    print(e)

the output is key1, key2,key3. The order is anything because of its unordered list.

For unpacking the dictionaries and set you to have to match the number of an element with a number of values

s = {1, 2, 3, 4, 5}
a, b, c, d, e = s

In the above example, If I have 5 elements in the set and I assign a,b,c=s it will throw an error

In this is not get values but it only iterates through keys.

Dictionaries and sets are unordered types. They are not like tuples or a list of ordered types.

What is the use of * in unpacking?

If we have iterable and every time we don’t want to iterate all values into a single element. For example

 A=[1,2,3,4]

In this, we want to unpack first value into a and rest it into b. how we do that by using slicing.

a=A[0]
b=A[1:] or a,b=A[0],A[1:] 

this is also called as parallel assignment.

This is done by using * operator.

A,*b=A 

It can work with any iterable types including unordered iterable also like dictionaries and sets.

star operator

But in this below code is not possible

a, *b, *c = A
print(a, b, c)

In the above example two * operator not worked and throws an error. but below code work perfectly.

a, b, *c = A
print(a, b, c)
a, *b, c = A
print(a, b, c)
a = [1, 2, 3]
b = [4, 5, 6]
c = (*a, *b)
print(c)
#this is also possible

What is the use of **?

IF I want to merge 2 dictionaries using * this single star operator it is not possible because it only takes key from that 2 dictionaries.

Combine 2 dictionaries

In this above screenshot see the output it prints only keys. For merging 2 dictionaries you need to use ** operator.

a = {'a': 1, 'b': 2}
b = {'a': 1, 'b': 3,'c':4}
c = {**a, **b}

print(c)

See the below image the output. Remember when you merge dictionaries whatever the entries present in the last one is overrides with previous one if keys are same else merge separately into one.

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Sagar Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

You may also like...