Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,083 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)