DEV Community

Piyush Darji
Piyush Darji

Posted on

Difference between (*a) and (a)

What is the difference of (*a) and (a) in this program???

def func(*a):
for i in range (0,len(a)-1):
if a[i]==3 and a[i+1]==3:
return True
return False
print(func([1,3,3]))

Output:- False

def func(a):
for i in range (0,len(a)-1):
if a[i]==3 and a[i+1]==3:
return True
return False
print(func([1,3,3]))

Output:-True

Top comments (2)

Collapse
 
sfiquet profile image
Sylvie Fiquet

The * syntax is for passing a variable number of arguments. Since the function is called with a single argument, a is a list containing a single item: [[1,3,3]] so len(a) is 1.

Whereas in the second version a is [1,3,3] and len(a) is 3.

There's a good explanation of *args and **kwargs on saltycrane.com/blog/2008/01/how-to....

Collapse
 
piyush6299 profile image
Piyush Darji

Thanks a lot