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

Gryphon Chandler
Gryphon Chandler
12,668 Points

How do I call a function on a flattened version of a list?

I have a function that takes in an integer. Then, it creates a list that is that long. It then needs to call a function on the entire list at the same time, as shown below:

def my_function(length):
    temp_list = make_list(length)
    return mystery(temp_list[0], temp_list[1], temp_list[2], temp_list[3] ...)

Essentially, the code needs to do this, but for every single item in temp_list. Also, I can't change make_list() or mystery().

1 Answer

Steven Parker
Steven Parker
229,005 Points

You just need the unpack operator ("*")

Also called the "splat" operator, it unpacks your list into individual arguments:

def my_function(length):
    temp_list = make_list(length)
    return mystery(*temp_list)