Python / dictionary> setdefault ()> Add if not in dictionary

@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic (No. 3118 / 12833) Handle Missing Keys with setdefault() and defaultdict()

The setdefault() function is like get(), but also assigns an item to the dictionary if the key is missing:

I've tried.

http://ideone.com/N2bmsU

away_team = { 'Tuvok': 1, "B'Elanna" : 2 }
print(away_team)
seven = away_team.setdefault("7of9", 3)
print(away_team)
print(seven)

run


{"B'Elanna": 2, 'Tuvok': 1}
{"B'Elanna": 2, 'Tuvok': 1, '7of9': 3}
3

Recommended Posts

Python / dictionary> setdefault ()> Add if not in dictionary
Avoid KeyError in python dictionary
Programmer disqualification problem 5 in Python if not solved in 1 hour (repost)
Why do you add a main ()-if statement in Python?
Python dictionary
[Python] dictionary
Python dictionary
Check if the URL exists in Python
Hash in Perl is a dictionary in Python
Tkinter could not be imported in Python
python> does not include the letters mm> if "mm" not in text: / print "not including mm"
Add words to MeCab's user dictionary on Ubuntu for use in Python
Python if statement
Quadtree in Python --2
CURL in python
[Python] Memo dictionary
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Check if the characters are similar in Python
Meta-analysis in Python
Unittest in python
[Python] Dictionary (hash)
Get multiple maximum keys in Python dictionary type
Discord in Python
Python basics: dictionary
Add quotation marks ">" for replying emails in Python3
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
To add a module to python put in Julialang
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
python memo-"if not A and B" was "if (not A) and B"
What to do if you get "Python not configured." Using PyDev in Eclipse
N-gram in python
[Python] if statement
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Use the LibreOffice app in Python (3) Add library
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python