Python Programming/Dictionaries

This lesson introduces Python dictionaries.

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

Readings

 * 1)  Dictionary (data structure)
 * 2) Python for Everyone: Dictionaries

Multimedia

 * 1) YouTube: Python for Informatics - Chapter 9 - Dictionaries
 * 2) YouTube: Python Dictionary
 * 3) YouTube: Python - Min, Max, and Sorting Dictionaries
 * 4) YouTube: Python Dictionaries Playlist

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: {'dog': 'Spot', 'fish': 'Bubbles', 'cat': 'Frisky'} dictionary['cat']: Frisky dictionary['dog']: Spot dictionary['fish']: Bubbles

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

Output: dictionary: {'cat': 'Frisky', 'fish': 'Bubbles', 'dog': 'Spot'} dictionary: {'cat': 'Frisky', 'fish': 'Bubbles', 'dog': 'Rover'} dictionary: {'cat': 'Frisky', 'dog': 'Rover'}

Multidimensional Dictionaries
Dictionaries may be nested.

Output: John's name: John Smith Jose's phone: 123-555-0102

Looping
Dictionary items may be accessed with a for loop.

Output: dictionary[dog]: Spot dictionary[fish]: Bubbles dictionary[cat]: Frisky

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: dictionary[cat]: Frisky dictionary[dog]: Spot dictionary[fish]: Bubbles

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[fish]: Bubbles dictionary[cat]: Frisky dictionary[dog]: Spot

Dictionary as Return Value
Functions may return multiple values using a dictionary.

Tutorials

 * 1) Complete one or more of the following tutorials:
 * 2) * LearnPython
 * 3) ** Dictionaries
 * 4) * TutorialsPoint
 * 5) ** Dictionary
 * 6) * Codecademy
 * 7) ** Lists and Dictionaries
 * 8) * Wikiversity
 * 9) ** Python/Dictionaries
 * 10) * Wikibooks
 * 11) ** A Beginner's Python Tutorial/Tuples, Lists, Dictionaries

Practice

 * 1) 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:
 * 2) 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:
 * 3) 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:
 * 4) Create a Python program that asks the user for a line of text that contains HTML tags, such as:     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 ). Use a user-defined function for the actual string processing, separate from input and output. For example:

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.
 * A pair of braces creates an empty dictionary: {}. Dictionary items may be added, updated, and deleted by key using the syntax  and.
 * Dictionaries may be nested and are accessed using the syntax
 * Dictionary items may be accessed with a  loop.
 * Dictionaries are unordered. Dictionary items may be displayed in key order by sorting a list of keys using the  function and the   method.
 * Dictionaries are unordered. Dictionary items may be displayed in value order by sorting a list of key-value pairs using the  function and the   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”.

Assessments

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