Dictionaries

Image result for dictionary

A dictionary in Python is a collection of unordered values accessed by key rather than by index. The keys have to be hashable: integers, floating point numbers, strings, tuples, and frozensets are hashable, while lists, dictionaries, and sets other than frozensets are not.

Lists organize their elements by position. This mode of organization is useful when you want to locate the first element, the last element, or visit each element in a sequence. However, in some situations, the position of data in a structure doesn’t matter. We’re interested in its association with some other element in the structure.

A dictionary organizes information by association, not position. In computer science, data structures organized by association are also called tables or association lists. In Python, a dictionary associates a set of keys with data values. A dictionary is written as a sequence of key/value pairs separated by commas. These pairs are called entries.

The entire sequence of entries is enclosed in curly brackets {}. A colon ; separates a key and its value. Here is an example:

A phone book: {‘Jenna’:’909-555-5555′, ‘Kenny’:’909-123-4567′}

Here is a list of common dictionary operations and their functions:

Leave a comment