Introduction:
- A view on a dictionary object, acts as a read-only reference to certain parts of the dictionary. These parts are keys, values and the items(key+value i,e., the name value pairs).
- Dictionary views allows operations like membership test, iteration, getting the length of the dictionary(number of keys==number of items==dictionary length) and getting the view members in reverse order.
- There are three types of dictionary views:
- dict_keys: Obtained by calling the keys() method.
- dict_values: Obtained by calling the values() method
- dict_items: Obtained by calling the items() method
Example:
# Example Python program that creates a dictionary view on the keys of # A dictionary of cities and their co-ordinates # Get a ditionary view on keys print("Type of dictionary view: %s"%type(keysView)); placeToFind = "Nili Fossae"; # Check for membership # Print each key in the view |
Output:
Type of dictionary view: <class 'dict_keys'> |