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

list functions doesn't need unpacking?

in the vid the sample code for def add(nums) needed the "" when it's for packing tuple:

def add(*nums): ... total = 0 ... for num in nums: ... total += num ... return total ... add(5,5) 10

however when i used list entries i found out that you don't need to use * in the add():

def add(nums): ... total = 0 ... for num in nums: ... total += num ... return total ... add([5,20]) 25

why is that?

1 Answer

Steven Parker
Steven Parker
231,269 Points

You only need the "splat" operator when the function will be passed several individual arguments and you want to treat them as a list in the function body.

If the function is passed an actual list as a single argument, you don't need it.