Jim,
"Get()" is useful when the programmer doesn't care if "eggs" is a key in the
dictionary, but a default value will act as a substitute for key:value pair.
In the "eggs" example this works because if there is no egg key then there are
0 eggs.Should the programmer want to do something different, e.g.
add an "eggs" key if there isn't one, then the "get" method will not meet the
need.
The programmer would have to write something like:
# begin code
if 'X' not in dict:
dict.update({'X':0})
continue
# end if
# end code
So "get()' may not meet the programmer's need in all situations;
If such is the case, then
the programmer can use "if "X" in dict: ...
If the programmer can use it, "get()"
is one line instead of two, less typing,
and probably noticeably faster if the programmer has a large dictionary.Should
the programmerhave several of these to write out, the "get()" will be less
tedious to type out than the "if 'X' in dict".
If the programmer is having a few or more such code snippets to type out,
the programmer might want to consider writing a function to handle the problem.
# code begin
def Dict_update(theKey, theDict, aValue = 0):
If theKey not in theDict.keys():
theDict.update({theKey:aValue})
return
# end function Dict_update()
# end code
This saves programmer time makes it easier to change when required to do so.
(i.e. the programmer changes the interior of the function rather than going
through severalplaces in her/his code that will have to separately change.
The one disadvantage to such a solution (i.e. using a function) is that
function calls can slow down the process considerably.
If the programmer is working with relatively small data sets then the extra
drag will probably not be noticeable.Either way one does it, the process should
complete within a small fraction of a second.
Jim, I am curious. What are you trying to do that "get" will not satisfy?
Hope that helps,
Jet (Jeffrey Thompson)
jthomp@xxxxxxxxxxx
-----Original Message-----
From: pythonvis-bounce@xxxxxxxxxxxxx [mailto:pythonvis-bounce@xxxxxxxxxxxxx] On ;
Behalf Of Jim
Sent: Wednesday, October 12, 2016 2:37 PM
To: pythonvis
Subject: [pythonvis] Dictionary Question, Using the dictionary.get function or
using if x in d
Hi,
I'm reading:
https://automatetheboringstuff.com/chapter5/
I'm not getting the point about the following piece of text.
Checking Whether a Key or Value Exists in a Dictionary
Recall from the previous chapter that the in and not in operators can
check whether a value exists in a list. You can also use these
operators to see whether a certain key or value exists in a
dictionary. Enter the following into the interactive shell:
Truespam = {'name': 'Zophie', 'age': 7}
'name' in spam.keys()
True'Zophie' in spam.values()
False'color' in spam.keys()
True'color' not in spam.keys()
False'color' in spam
'I am bringing 2 cups.'picnicItems = {'apples': 5, 'cups': 2}
'I am bringing ' + str(picnicItems.get('cups', 0)) + ' cups.'
'I am bringing 0 eggs.''I am bringing ' + str(picnicItems.get('eggs', 0)) + ' eggs.'
Traceback (most recent call last):picnicItems = {'apples': 5, 'cups': 2}
'I am bringing ' + str(picnicItems['eggs']) + ' eggs.'