GithubHelp home page GithubHelp logo

project2: What does "**" mean? about pycar HOT 3 CLOSED

ireapps avatar ireapps commented on July 28, 2024
project2: What does "**" mean?

from pycar.

Comments (3)

tommeagher avatar tommeagher commented on July 28, 2024

That part of the day was designed by @hbillings, so Heather, would you like to take a swing at explaining the ** dict unpacking for Jenny?

from pycar.

hbillings avatar hbillings commented on July 28, 2024

Oof, sorry about the late response on this! Also...oh man, reading my own code sucks. Let me try to remember what I was doing there...

So this is an example of kwargs, which stands for "keyword arguments." If you look at line 15, you'll see I'm doing this there:
deets_plus_job = dict(my_deets, **{'employer': 'Northwestern University', 'boss': 'Joe Germuska'})

Alternately, I could write this in the same way I wrote what's on line 13:
deets_plus_job = dict(my_deets, 'employer' = 'Northwestern University', 'boss'='Joe Germuska')

You can see from that example that the double asterisks mean, "Take whatever is in this dictionary, and add it to the first dictionary."

The helpful part about this technique (which is called "unpacking") comes about when you're writing code that is going to have to deal with either a lot of values or a variable number of values. So let's go back to the original line:

merged_data.append(dict(dict1[key], **dict2[key]))

dict1, IIRC, is a dict of dicts, meaning that dict1[key] is itself a dict. If memory serves, both dict1 and dict2 are dicts of player details, but each one has different information. They share a common key: the player ID. So we want to join them together based on that ID.

The line in question finds the dict that corresponds to dict1[key], set that as the key, and then unpack or expand the values of the second dict so that they can be added.

There is perhaps a more clear explanation with examples on how unpacking works here: http://programmers.stackexchange.com/a/131415

from pycar.

jtmx avatar jtmx commented on July 28, 2024

Thanks Heather. Unpacking makes much more sense now - a short cut for adding dicts together so you don't have to rewrite the entire thing. Many thanks.

from pycar.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.