The Python Dictionary (dict): An Overview
Among the built-in Python data types is a very versatile type called a dictionary. The dictionary is similar to lists and tuples because they act as storage units for other objects or variables you’ve created. Dictionaries are different from lists and tuples because the group of objects they hold aren't in any particular order, but rather each object has its own unique name, commonly known as a key.
The objects or values stored in a dictionary can basically be anything (even the nothing type defined as None
), but keys can only be immutable type-objects. e.g., strings, tuples, integers, etc.
Note: Immutable types that store objects like tuples can only contain other immutable types to be dictionary keys.
Creating a Python Dictionary
Creating a dictionary is easy, here is an empty one:
[python]
emptydict = {}
[/python]
Here is one with a few values (note the {key1: value1, key2: value2, ...}
structure) :
[python]
smalldict = {
'dlist': [],
'dstring': 'pystring',
'dtuple': (1, 2, 3)
}
[/python]
You can also use the dictionary constructor (note with this method, keys can only be keyword strings):
[python]
smalldict = dict(dlist=[], dstring='pystring', dtuple=(1, 2, 3))
[/python]
Get a Python Dictionary's Values
To get one of the values, just use its key:
[python]
>>> smalldict = {
'dlist': [],
'dstring': 'pystring',
'dtuple': (1, 2, 3)
}
>>> smalldict['dstring']
'pystring'
>>> smalldict['dtuple']
(1, 2, 3)
[/python]
Remove a Python Dictionary's Key
To remove a key: value pair, simply delete it with the del
keyword:
[python]
>>> del smalldict['dlist']
>>> smalldict
{'dstring': 'pystring', 'dtuple': (1, 2, 3)}
[/python]
Add a Key to a Python Dictionary
Or to assign a new key, just use the new key:
[python]
>>> smalldict['newkey'] = None
>>> smalldict
{'dstring': 'pystring', 'newkey': None, 'dtuple': (1, 2, 3)}
[/python]
Overwriting Python Dictionary Key-value Pairs
Note: All the keys in a dictionary are unique, so if you assign a value to an existing key, it will be overwritten! For example:
[python]
>>> smalldict['dstring'] = 'new pystring'
>>> smalldict
{'dstring': 'new pystring', 'newkey': None, 'dtuple': (1, 2, 3)}
[/python]
Swap a Key and Value in a Python Dictionary
If you want to swap or change the key for a value, you can create the new key with the value, then simply delete the old key:
[python]
>>> smalldict['changedkey'] = smalldict['newkey']
>>> del smalldict['newkey']
>>> smalldict
{'dstring': 'new pystring', 'changedkey': None, 'dtuple': (1, 2, 3)}
[/python]
Search a Python Dictionary by Key
To check if a key exists in the dictionary, just use key in smalldict:
[python]
>>> 'dstring' in smalldict
True
[/python]
Note: This does not check whether a value exists in the dictionary, only a key:
[python]
>>> 'new pystring' in smalldict
False
[/python]
Search a Python Dictionary by Value
To check for a given value, you can use one of the dictionary's standard methods, values
.
[python]
>>>'new pystring' in smalldict.values()
True
[/python]
There are many more useful methods and recipes for dictionary objects, but those are the basics.
Remember, keys don't have to be strings, and can be any immutable object you can create—and values can be just about anything, including other dictionaries. Don't be afraid to be creative and discover what you can do with them!