Ad Code

Exclusive Unpacking in Python Using Splat Operator

The splat operator, in python, changes the format of the display of the output. The asterisk in python is called a spalt operator.

There are two types of splat operators. Those are single and double. Here are the best examples of those.

1. Single splat operator


Consider, for example, this code: 

abc = [1,2,3,4] 


print(abc) In this example, our output is: [1, 2, 3, 4]


What if you didn't want the list output in list format? What if all you wanted was the list of values to be written to the output console? You could write them using a loop and one of the output functions, but Python prefers an easier way:

print(*abc) 1 2 3 4


Splat Operators



2. Double splat operator


Here, I have written a function:

def func(x,y,z): 
      return x + y + z

print(func(**d))

It will show '6' as output. Since, I have assigned values for x,y, and z in a dictionary. So by using a double splat operator you assign values to the function.

d = { 
'x': 1, 
'y': 2, 
'z': 3 
}


Related posts

Post a Comment

0 Comments