Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Tuples Tuple Swapping

what is *args and what does it do?

What does args do and what does it mean I completly don't get . please help

3 Answers

Steven Parker
Steven Parker
231,236 Points

The "packing/unpacking" operator (also known as "gather/spread" or just "splat") does one of two things based on where it is used.

In a function definition, it causes individual arguments passed to the function to be gathered together into a single list with the name given after the "*" ("args" in your example):

def foo(*args) :point_left: this definition, and this call :point_right: foo(a, b, c)
...would be the same as...
def fum(args) :point_left: this definition with this call :point_right: fum( [a,b,c] )

Elsewhere in the code, it causes a list to be expanded into a set of individual arguments:

bunch = [1, 2, 3]
x = somefunc(*bunch)  #  this is the same as:  x = somefunc(1, 2, 3);
diego cortes
diego cortes
1,421 Points

Thank you Steven, your examples make it easier for me to understand the concept

Thanks! I get it now.

Thank you!