# Python Programming/Dictionaries

This lesson introduces Python dictionaries.

## Objectives and Skills

Objectives and skills for this lesson include:
* Data Structures
** Dictionary

## Readings

* Wikipedia: Dictionary (data structure)
* Python for Everyone: Dictionaries

## Multimedia

* YouTube: Python for Informatics - Chapter 9 - Dictionaries
* YouTube: Python Dictionary
* YouTube: Python - Min, Max, and Sorting Dictionaries
* YouTube: Python Dictionaries Playlist

## Examples
### Dictionaries

Dictionaries are collections that are indexed by keys, which can be any immutable type. It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary). Placing a comma-separated list of key:value pairs within braces adds initial key:value pairs to the dictionary.
Output:

### Dictionary Updates

A pair of braces creates an empty dictionary:. Dictionary items may be added, updated, and deleted by key.
Output:

### Multidimensional Dictionaries

Dictionaries may be nested.
Output:

### Looping

Dictionary items may be accessed with a for loop.
Output:

### Sorting by Key

Dictionaries are unordered. Dictionary items may be displayed in key order by sorting a list of keys using the sorted function and the dictionary.keys method.
Output:

### Sorting by Value

Dictionaries are unordered. Dictionary items may be displayed in value order by sorting a list of key-value pairs using the sorted function and the dictionary.get method.
Output:

### Dictionary as Return Value

Functions may return multiple values using a dictionary.

## Activities
### Tutorials

* Complete one or more of the following tutorials:
** LearnPython
*** Dictionaries
** TutorialsPoint
*** Dictionary
** Codecademy
*** Lists and Dictionaries
** Wikiversity
*** Python/Dictionaries
** Wikibooks
*** Wikibooks: A Beginner's Python Tutorial/Tuples, Lists, Dictionaries

### Practice

* Create a Python program that asks the user to enter grade scores. Start by asking the user how many scores they would like to enter. Then use a loop to request each score and add it to a dictionary, counting how many times each score is entered. Finally, use a function to display the dictionary of entered scores sorted in descending order and a histogram showing how many times each score was entered. Include try and except to handle input errors. For example: 100: * 90: *** 80: ***** 70: **
* Create a Python program that asks the user to enter a line of comma-separated grade scores. Use the string split method to parse the line and add each score to a dictionary, counting how many times each score is entered. Finally, use a function to display the list of entered scores sorted in descending order and a histogram showing how many times each score was entered. Include try and except to handle input errors. For example: 100: * 90: *** 80: ***** 70: **
* Create a Python program that asks the user to enter a line of text. Use the string split method to parse the line and add each word to a dictionary, counting how many times each word is entered. Convert words to lower case and remove any punctuation to prevent duplicate or invalid results. Finally, use a function to display the list of entered words sorted in alphabetical order and a histogram showing how many times each word was entered. Include try and except to handle input errors. For example: cat: * hat: * in: * the: **
* Create a Python program that asks the user for a line of text that contains HTML tags, such as: <p><strong>This is a bold paragraph.</strong></p> Use string methods to search for and remove all HTML tags from the text, saving each removed tag in a dictionary. Print the untagged text and then use a function to display the list of removed tags sorted in alphabetical order and a histogram showing how many times each tag was used. Include error handling in case an HTML tag isn't entered correctly (an unmatched < or >). Use a user-defined function for the actual string processing, separate from input and output. For example: </p>: * </strong>: * <p>: * <strong>: *

## Lesson Summary
### Dictionary Concepts

* A dictionary or associative array is a data type composed of a collection of (key: value) pairs, such that each possible key appears at most once in the collection.
* Dictionary operations include:
** the addition of a pair to the collection
** the removal of a pair from the collection
** the modification of an existing pair
** the lookup of a value associated with a particular key
* Dictionaries are typically implemented in programming languages as either a hash table or a search tree.
* A hash table uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found.
* A hash function is any function that can be used to map data of arbitrary size to data of fixed size.
* A good hash function should map the expected inputs as evenly as possible over its output range.

### Python Dictionaries

* Dictionaries are collections that are indexed by keys, which can be any immutable type.
* It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary).
* Placing a comma-separated list of key:value pairs within braces adds initial key:value pairs to the dictionary using the syntax dictionary = {key: value, key: value}.
* A pair of braces creates an empty dictionary:. Dictionary items may be added, updated, and deleted by key using the syntax dictionary[key] = value and del dictionary[key].
* Dictionaries may be nested and are accessed using the syntax dictionary[key][subkey]
* Dictionary items may be accessed with a for loop.
* Dictionaries are unordered. Dictionary items may be displayed in key order by sorting a list of keys using the sorted function and the dictionary.keys method.
* Dictionaries are unordered. Dictionary items may be displayed in value order by sorting a list of key-value pairs using the sorted function and the dictionary.get method.

## Key Terms
### dictionary

    A mapping from a set of keys to their corresponding values.

### hashtable

    The algorithm used to implement Python dictionaries.

### hash function

    A function used by a hashtable to compute the location for a key.

### histogram

    A set of counters.

### implementation

    A way of performing a computation.

### item

    Another name for a key-value pair.

### key

    An object that appears in a dictionary as the first part of a key-value pair.

### key-value pair

    The representation of the mapping from a key to a value.

### lookup

    A dictionary operation that takes a key and finds the corresponding value.

### nested loops

    When there are one or more loops “inside” of another loop. The inner loop runs to completion each time the outer loop runs once.

### value

    An object that appears in a dictionary as the second part of a key-value pair. This is more specific than our previous use of the word “value”.

## Review Questions

* A dictionary or associative array is _____. A dictionary or associative array is a data type composed of a collection of (key: value) pairs, such that each possible key appears at most once in the collection.
* Dictionary operations include: Dictionary operations include: the addition of a pair to the collection the removal of a pair from the collection the modification of an existing pair the lookup of a value associated with a particular key
* Dictionaries are typically implemented in programming languages as _____. Dictionaries are typically implemented in programming languages as either a hash table or a search tree.
* A hash table uses _____. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found.
* A hash function is _____. A hash function is any function that can be used to map data of arbitrary size to data of fixed size.
* A good hash function should _____. A good hash function should map the expected inputs as evenly as possible over its output range.
* Python dictionaries are _____. Python dictionaries are collections that are indexed by keys, which can be any immutable type.
* It is best to think of a dictionary as _____. It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary).
* Placing a comma-separated list of key:value pairs within braces adds initial key:value pairs to the dictionary using the syntax _____. {{{2}}}
* A pair of braces creates an empty dictionary:. Dictionary items may be added, updated, and deleted by key using the syntax _____. {{{2}}}
* Dictionaries may be nested and are accessed using the syntax _____ Dictionaries may be nested and are accessed using the syntax dictionary[key][subkey]
* Dictionary items may be accessed with a _____ loop. Dictionary items may be accessed with a for loop.
* Dictionaries are unordered. Dictionary items may be displayed in key order by _____. Dictionaries are unordered. Dictionary items may be displayed in key order by sorting a list of keys using the sorted function and the dictionary.keysmethod.
* Dictionaries are unordered. Dictionary items may be displayed in value order by _____. Dictionaries are unordered. Dictionary items may be displayed in value order by sorting a list of key-value pairs using the sorted function and the dictionary.get method.

## Assessments

* Flashcards: Quizlet: Python Dictionaries
* Flashcards: Quizlet: Python Dictionary Methods
* Quiz: Quizlet: Python Dictionaries
* Quiz: Quizlet: Python Dictionary Methods

## See Also

* Python.org: Data Structures
* SoloLearn: Python
* Open Book Project: Python 3 - Dictionaries
* Dictionaries in Python