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

Ceil-Ian Maralit
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ceil-Ian Maralit
Front End Web Development Techdegree Graduate 19,422 Points

Why pass it as a dictionary when we can just pass it as key/value pairs?

Hi! I don't understand why it is passed as a dictionary when we can just pass it as key/value pairs and still work the same. Is it just for the sake of clarity?

Here is the their example:

def unpacker(first_name=None, last_name=None): 
    if first_name and last_name:
        print("Hi {} {}!".format(first_name, last_name))
    else:
        print("Hi no name!")

unpacker(**{"first_name": "Ceil", "last_name": "Cisneros"})

Here is my test:

def unpacker(first_name=None, last_name=None): 
    if first_name and last_name:
        print("Hi {} {}!".format(first_name, last_name))
    else:
        print("Hi no name!")

unpacker(first_name = "Ceil", last_name = "Cisneros")

Thanks in advance!

--Ceil

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 27,654 Points

This challenge is setting you up to have a better understanding of argument passing. In particular, in your Python journey you will run into "args" and "kwargs" as arguments passed into your methods and objects. "kwargs" stands for keyword arguments and can be treated similarly to a dictionary.

Here is a nice blog article from one of my favorite hosting providers-- Digital Ocean:

https://www.digitalocean.com/community/tutorials/how-to-use-args-and-kwargs-in-python-3

Thanks, Jeff! That article made the video make sense. (It also made it clear that **kwargs is more useful than just putting elements into and out of dictionaries.) Thanks again!

Deividas Paulauskas
Deividas Paulauskas
3,661 Points

Thanks, Jeff, that article was very useful and easy to understand!