1

capitals[add] = capitals.get(add, "?") Im confused as to what that does. Can anyone explain to me what its doing?

  • 2
    Does this answer your question? [Understanding .get() method in Python](https://stackoverflow.com/questions/2068349/understanding-get-method-in-python) – Matt May 21 '21 at 02:57
  • Still not getting it. – Vincent Ludwig May 21 '21 at 03:00
  • 1
    Your edit is unnecessary because the fact that the answer is 11 years old is irrelevant; the way the method works hasn't changed. A dictionary is a mapping of *keys* to *values*. So you might have `myDict = { 'V': 'Vincent' }` for example. If you want to get the value you would call `myDict['V']` or `myDict.get('V')`, which will give you `'Vincent'` as the result. The second parameter simply lets you use a default value in case the dictionary doesn't have an entry. So in the example I used, calling `myDict.get('M', '?')` would return `'?'`since there is no entry for `'M'`. – Matt May 21 '21 at 03:11
  • Thanks. I think I'm getting it now. I just was confused that you could add keys and values to a dictionary. – Vincent Ludwig May 21 '21 at 03:13
  • Your question would have been improved by an explanation of what exactly was confusing to you about the code. Do you know what the different parts do? Do you know what `some_dict[key] = whatever` means? Do you know what `some_dict.get(key, "foo")` means? Is it just their combination that confuses you? – Blckknght May 21 '21 at 03:17
  • I just didn't know how how it was adding a key but I get it now! – Vincent Ludwig May 21 '21 at 03:18

3 Answers3

1

The assignment in that code is equivalent to this alternative, which explicitly tests for the existence of the key held in the variable add in the dictionary:

if add not in capitals:
    capitals[add] = "?"

The original code you asked about uses different logic by utilizing the dict.get method to get either the existing value (if there is one) for the key add, or the string "?" if that key doesn't exist in the dictionary. In either case, it unconditionally updates the dictionary (either with the same value it had, or the new default). Afterwards, you can be confident that add is always a key in the dictionary with some value.

A better solution than either your original or my alternative version of it above would be:

capitals.setdefault(add, "?")

This is likely to preform a little better because it's all in one method, so there's no need for the dictionary to need to hash the key twice.

Blckknght
  • 100,903
  • 11
  • 120
  • 169
1

Here's an example to show how the get function works on a dictionary. Let's say your dictionary looks like this:

capitals = {
    'MA': 'Boston',
    'RI': 'Providence',
    'NY': 'Albany'
}

If you use the get function on a valid key, you'll get the value back. For example:

>>> add = 'RI'
>>> capitals.get(add, "?")
'Providence'

If you try the get function on a key that isn't present in the dictionary, then you will get back the missing key value (a '?' in your example).

>>> add = 'TX'
>>> capitals.get(add, "?")
'?'

So if you set capitals[add] = capitals.get(add, "?") it would either create a new key with a value of "?" (if the key doesn't exist) or update an existing key with the same value it already has.

There's other documentation on the web if you search for "python get function". Good luck!

Carl Cervone
  • 1,103
  • 8
  • 10
1

Since you seem to be unable to understand the given response, I'll break it down for you.

The get() method of a dictionary does the following:

Checks the dictionary to see if the dictionary has a key corresponding to the first parameter. Then, it does one of the following:

  1. If there is a key that matches the given one, then it returns the corresponding value.
  2. If there is no matching key, then, if the second parameter is provided, it will return that value.

Now let's look at your question with the following two sample dictionaries:

capitals_1 = {
  "UK": "London",
  "USA": "Washington DC",
  "India": "New Delhi"
}
capitals_2 = {
   "Brazil": "Brasilia",
   "Australia": "Canberra",
   "Russia": "Moscow"
}

add = "India"

For capitals_1, "India" is already a key, so executing the above code will return the value corresponding to "India", which is "New Delhi". For capitals_2, "India" is not a key, so executing the above code will return the default value specified, which is "?".

With these values obtained, the code then sets the value corresponding to "India" to whatever you got, so if "India" is already a key, then no change, but, if, like the second example, if India is not a key, then the dictionary will have the additional key-value pair of "India":"?".

Dharman
  • 30,962
  • 25
  • 85
  • 135
Prometheus
  • 618
  • 1
  • 7
  • 23
  • 1
    Yeah! I got lots of very detailed responses! I now understand it very well! (: – Vincent Ludwig May 21 '21 at 03:22
  • So when we say capitals[add] = capitals.get("", "keys") i dont have to add anything in the first get parameter since the index of capitals is overriding it? – Vincent Ludwig May 21 '21 at 03:43
  • You have to provide the first parameter by default, which should be `add` in your case. Else, this `""` key won't be in your dictionary and will just return "keys" by default, for all cases. – Prometheus May 21 '21 at 03:45