A dictionary is a data structure that consists of a collection of key-value pairs. Dictionaries are one of the most widely used and important built-in data structures in Python. In other languages they might be called hash maps, associative arrays or associative memories. The keys must be unique within a dictionary. The values need not be unique. Key and value are Python objects. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys.
A dictionary’s keys must be immutable. Immutable keys include, but are not limited to, integers, floats, tuples and strings.
Dictionaries are ordered, but that depends. As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.
What does unordered mean? It means you can’t access them by referencing a positional index. Because dictionaries are unordered, you’ll sometimes find that the order of the entries can change as you’re working with them. If the order of your data is important, it’s better to use an ordered data structure like a list. The value in the key-value pair must be a single value. If you have multiple values, you can use a list.
Dictionaries are changeable, meaning that we can change, add or remove items after the dictionary has been created.
Creating Dictionaries
They are instantiated with curly braces { } (curly brackets) or the dict() function. Dictionaries use curly brackets { }. You can create an empty dictionary like this empty_dict = {} or like this empty_dict = dict().
Here’s an example.
# Ontaario Canada telephone area codes ont_codes = { '226': 'London, Kitchener–Waterloo, Windsor, and most of Southwestern Ontario, overlays with 519 and 548', '249': 'Most of Northeastern and Central Ontario, overlays with 683 and 705', '289': 'Niagara Region, Hamilton, Halton Region, Peel Region, York Region and Durham Region, overlays with 905 and 365', '343': 'Ottawa and surrounding Eastern Ontario, overlays with 613', '365': 'Niagara Region, Hamilton, Halton Region, Peel Region, York Region and Durham Region, overlays with 905 and 289', '416': 'Toronto, overlays with 647 and 437', '437': 'Toronto, overlays with 647 and 416', '519': 'London, Kitchener–Waterloo, Windsor, and most of Southwestern Ontario, overlays with 226 and 548', '548': 'London, Kitchener–Waterloo, Windsor, and most of Southwestern Ontario, overlays with 519 and 226', '613': 'Ottawa and surrounding Eastern Ontario, overlays with 343', '647': 'Toronto, overlays with 416 and 437', '683': 'Most of Northeastern and Central Ontario, overlays with 249 and 705', '705': 'Most of Northeastern and Central Ontario, overlays with 249 and 683', '807': 'Most of Northwestern Ontario', '905': 'Niagara Region, Hamilton, Halton Region, Peel Region, York Region and Durham Region, overlays with 289 and 365' }
We can use the dict() function to create a dictionary.
zoo = dict( [ ['Africa','cheeta, lion, ostrich, baboon'], ['Americas','boa constrictor, tamarin, macaw'] ] ) zoo['Africa']
A dictionary’s keys must be immutable (unchangeable). Immutable keys include, but are not limited to, integers, floats, tuples and strings. Lists, sets, and other dictionaries are not included in this category, since they are mutable.
Dictionaries are not ordered. You cannot use an index to look up something. Dictionaries are not ordered and have no index. It would be better to use a list if your data’s order is important.
Appending to Dictionaries
To add to a dictionary, simply specify the dictionary name and it’s new value.
Creating DataFrames with Dictionaries
A DataFrame is a data structure in pandas. It’s similar to a table or a spreadsheet. In Python (with pandas imported) to manually create a DataFrame, start with a dictionary of equal-length lists.
import pandas as pd data = {'firstname': ['Bob', 'Sally', 'Suzie', 'Rohan'], 'amount': [12, 67, 33, 41]} df = pd.DataFrame(data)
More Information
Here is an article on dictionaries.