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 Python Collections (2016, retired 2019) Dictionaries Packing and Unpacking Dictionaries

Why when I try to pass a value to *arg it doesn't get passed to it.

I'm trying to catch second_color outside of the packing, and pass "I'm a tuple" to *args and print out both args and kwargs. Here's my example:

def color(second_color = None, *args, **kwargs): print(kwargs) print(args)
color(second_color= "Brown", "I'm a tuple", first_color= "Red", last_color= "Blue")

SyntaxError: positional argument follows keyword argument

1 Answer

Kent Åsvang
Kent Åsvang
18,823 Points

You need to remove the "second_color"-keyword from your function-call.

    color("Brown", "I'm a tuple", first_color= "Red", last_color= "Blue")

You can read about why here if you are interested

Also, remember that kwargs are dictionaries. So if you want to print them out you should use dict().items() and a for-loop:

    for item in kwargs.items():
        print(item)

for the args you can just use a basic for-loop:

    for item in args:
        print(items)

I hope this helped