Master this deck with 20 terms through effective study methods.
Generated from uploaded pdf
A dictionary in Python is a built-in data type that stores data in key-value pairs. Each key is unique and is used to access its corresponding value. Dictionaries are defined using curly braces {} with keys and values separated by a colon, e.g., {'key': 'value'}.
An empty dictionary can be created in Python using either the dict() constructor or by using curly braces. For example, you can create an empty dictionary with captains = {} or captains = dict().
You can use the 'in' keyword to check if a key exists in a dictionary. For example, 'if key in dictionary:' will return True if the key is present.
Items can be added to a dictionary using square bracket notation. For example, dictionary['new_key'] = 'new_value' will add a new key-value pair to the dictionary.
If you try to access a key that does not exist in a dictionary, Python will raise a KeyError. To avoid this, you can use the get() method, which returns None or a specified default value if the key is not found.
You can iterate over a dictionary using a for loop. For example, 'for key, value in dictionary.items():' allows you to access both keys and values during the iteration.
The del statement is used to delete a key-value pair from a dictionary. For example, 'del dictionary[key]' will remove the specified key and its associated value from the dictionary.
You can create a dictionary with initial values using the dict() function by passing keyword arguments. For example, dict(Enterprise='Picard', Voyager='Janeway') creates a dictionary with two key-value pairs.
A list is an ordered collection of items that can be accessed by their index, while a dictionary is an unordered collection of key-value pairs accessed by keys. Lists allow duplicate values, whereas dictionary keys must be unique.
Nested dictionaries are dictionaries that contain other dictionaries as values. They are useful for modeling complex data structures, such as representing hierarchical data or structured data like spreadsheets.
KeyError is an exception raised when trying to access a key that does not exist in a dictionary. It indicates that the requested key is not found, prompting the programmer to handle the situation appropriately.
You can safely retrieve a value from a dictionary using the get() method, which allows you to specify a default value to return if the key does not exist. For example, dictionary.get('key', 'default_value') will return 'default_value' if 'key' is not found.
When you iterate over a dictionary's keys using 'for key in dictionary:', you will get each key in the dictionary one at a time, allowing you to access the corresponding values using the keys.
You can merge two dictionaries using the update() method, which adds key-value pairs from one dictionary to another. Alternatively, in Python 3.9 and later, you can use the merge operator '|', e.g., merged_dict = dict1 | dict2.
The items() method returns a view object that displays a list of a dictionary's key-value tuple pairs. This allows for easy iteration over both keys and values in a dictionary.
A shallow copy of a dictionary creates a new dictionary object but does not create copies of nested objects; it only copies references. A deep copy creates a new dictionary and recursively copies all objects found in the original dictionary.
You can remove all items from a dictionary using the clear() method, which empties the dictionary but keeps the dictionary object itself intact.
The setdefault() method returns the value of a specified key if it exists; if not, it inserts the key with a specified default value. This is useful for initializing keys with default values.
You can find the length of a dictionary using the len() function, which returns the number of key-value pairs in the dictionary.
Common use cases for dictionaries include storing configuration settings, counting occurrences of items, representing structured data, and implementing lookup tables for fast data retrieval.