A dictionary is a mutable collection of many values. Values are associated with keys. Unlike lists , dictionaries aren’t indexed and keys can be in any order. Dictionaries are a collection of key-value pairs.
- Integers or strings can be used for keys.
>>> myCat = {'size': 'fat', 'color': 'gray', 'disposition': 'loud'}
>>> myCat
{'size': 'fat', 'color': 'gray', 'disposition': 'loud'}
>>> myCat['size']
'fat'
>>> 'My cat has ' + myCat['color'] + ' fur.'
'My cat has gray fur.'
>>> spam = {12345: 'Luggage Combination', 42: 'The Answer'}
- Python 3.7+ remembers order of insertion of key-value pairs, but this shouldn’t be relied on.