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) 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,434 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 28,722 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!