Python dictionaries
grouping objects into a collection of unordered but changeable key-value pairs
2025-11-05 14:55
// updated 2025-11-06 18:59
// updated 2025-11-06 18:59
A dictionary in Python consists of a collection of unordered, key-value pairs:
- the collection denoted by a pair of curly braces
{ } - each key-value pair with
- the key as a string or a number
- separated by a colon
- the value with any data type
- each value accessed via its key string using square bracket notation
- alas, we cannot use numeric indices like in lists and tuples
vocabulary = {
'english' : ['Hello', 'Thanks', 'Goodbye'],
'french' : ['Bonjour', 'Merci', 'Au revoir'],
'german' : ['Hallo', 'Danke', 'Tschüss'],
'swedish' : ['Hej', 'Takk', 'Hej då']
}
print(vocabulary['french'])
# ['Bonjour', 'Merci', 'Au revoir']
# dictionaries don't need to include just words
tax_rates = {
'taxistan' : {
'federal': 0.20,
'provincial': 0.05
},
'rolimpia' : 0.24,
'manduria' : 0.22,
'wawaland' : 0.19
}
print(tax_rates['manduria'])
# 0.22
print(tax_rates['taxistan']['federal'])
# 0.20
print(tax_rates[0])
# KeyErrorDictionary length
Similar to strings, lists and tuples, we can also find a dictionary's length using len(dictionary) built-in method:
tax_rates = {
'taxistan' : {
'federal': 0.20,
'provincial': 0.05
},
'rolimpia' : 0.24,
'manduria' : 0.22,
'wawaland' : 0.19
}
print(len(tax_rates))
# 4
print(len(tax_rates['taxistan']))
# 2Dictionary update
We can update a dictionary using the update built-in method and by passing another dictionary into the argument:
tax_rates = {
'taxistan' : {
'federal': 0.20,
'provincial': 0.05
},
'rolimpia' : 0.24,
'manduria' : 0.22,
'wawaland' : 0.19
}
new_tax_rates = {
'newyland' : 0.11,
'wawaland' : 0.21
}
tax_rates.update(new_tax_rates)
print(tax_rates)
# {'taxistan': {'federal': 0.2, 'provincial': 0.05}, 'rolimpia': 0.24, 'manduria': 0.22, 'wawaland': 0.21, 'newyland': 0.11}Note that if a key in the new dictionary already exists in the old dictionary, the new key will overwrite the key-value pair in the old dictionary!
Getting lists of key names and key values
By chaining the built-in keys() and values() methods to a dictionary, we can get a list of key names and key values, respectively:
tax_rates = {
'taxistan' : {
'federal': 0.20,
'provincial': 0.05
},
'rolimpia' : 0.24,
'manduria' : 0.22,
'wawaland' : 0.19
}
print(tax_rates.keys())
# dict_keys(['taxistan', 'rolimpia', 'manduria', 'wawaland'])
print(tax_rates.values())
# dict_values([{'federal': 0.2, 'provincial': 0.05}, 0.24, 0.22, 0.19])Summary
We can think of a dictionary as:
- an unordered collection of one or more mutable (changeable) items:
- each indexed by a key accompanied by a value
- the value can take on any type, including sub-dictionaries
- accessible via key names (but not ordered indices)
- capable of update via another dictionary
- each indexed by a key accompanied by a value
- denoted by curly braces and key-value pairs
{ key1: value1, ..., keyN: valueN }
Applications
Great uses for dictionaries include collections of:
- objects whose values change frequently but their order doesn't matter, e.g.:
- items and their prices
- foreign currencies and their exchange rates