Course Content
Introduction to Python
Dictionary Methods
As you may have noticed, dictionaries differ from lists and tuples. So do their methods. Let's consider them.
len(d)
- returns the length of dictionaryd
(number of pairskey:value
)d.copy()
- returns a copy of dictionaryd
d.items()
- returns all pairs (key, value
) in dictionaryd
d.keys()
- returns all keys of dictionaryd
d.values()
- returns all values of dictionaryd
How do you add new data to a dictionary? Unlike lists or tuples, neither .append()
/.extend()
methods nor concatenation works. Since dictionaries store data in pairs, we can add new data by keys:
d[k] = e
- sets valuee
to the keyk
. If keyk
is in existing keys, then the value will be rewritten
For instance, traditionally, let's update our dictionary with two countries:
Section 4.
Chapter 12