In programming, a dictionary (or associative container, symbol table, map) is a data type that has a collection of key-value pairs. Items within dictionaries are unordered.
Designing efficient data structures to implement dictionaries is difficult (called the dictionary problem), but can be done with hash tables and search trees. Language support of dictionaries is mixed. In Python, they are called dictionaries. In C++ and Java, they’re called maps.
Python
Dictionaries in Python are declared with curly brackets (see above for an example). Some associated methods are:
keys()
andvalues()
return a “list” of associated data.get('key', fallback)
grabs a key’s value. If it doesn’t exist, it returns the second argument.setdefault('key', value)
sets a default value for a given key, even if it doesn’t exist.
C++
The C++ map
is a part of the Standard Template Library as std::map
, implemented with a red-black tree (as a balancing BST).
The std::map
iterator advances as an in-order traversal of the