The dict
class in Python provides many useful methods, some of which are introduced in the concept exercise for dictionaries.
This concept tackles a few more:
dict.setdefault()
automatically adds keys without throwing aKeyError
.dict.fromkeys(iterable, <default value>)
creates a newdict
from any number of iterables..keys()
,.values()
, and.items()
provide convenient iterators.sorted(<dict>.items())
. can easily re-order entries in adict
.dict_one.update(<dict_two>)
updates onedict
with overlapping values from anotherdict
.dict | other_dict
anddict |= other_dict
merges or updates twodict
s via operators.reversed(dict.keys())
,reversed(dict.values())
, orreversed(dict.items())
produce reversed views.<dict>.popitem()
removes and returns akey
,value
pair.