Python

Constant dripping wears away a stone

Dictionaries


Dictionaries in Python are used to store a collection of data. The items in a dictionary are presented in key:valuepairs. The items are enclosed with curly brackets and are separated with commas. To create an empty dictionary, useDict_Name = dict().

  • Dictionary Properties:
    • Ordered: Dictionaries are ordered. The items in a dictionary have a defined order. A dictionary item can be accessed by referring to the key of the item.
    • Changable: Dictionary items are changable. The value of an item can be changed by referring to its key or by using theupdate()method (discussed below). A new item can be added to the dictionary by using a new key and assigning it a value.
    • No Duplicates: Dictionaries can not have items with the same keys.
  • Access Dictionary Items:
    • Using key: A dictionary item can be accessed by referring to its key inside square brackets.
    • Using get() method: A dictionary item can be accessed by using theget()method discussed below.
  • Dictionary Methods:
    • clear(): Removes all items from the given dictionary.
    • copy(): Returns a new dictionary which is a copy of the given dictionary.
    • fromkeys(keys[, value]): Returns a dictionary with the specified keyskeysandvalue. If valueis not given, use the default value None.
    • get(key[, value]): Returns the value of the item in the given dictionary with the specified keykey. If no item exists with the specified key, returns the specified valuevalueif it is given; otherwise, returns None.
    • items(): Returns a view object calleddict_itemswhich presents a list that contains each key-value pair of the given dictionary as a tuple. When the dictionary is updated, the object is also updated.
    • keys(): Returns a view object calleddict_keysthat presents a list that contains all keys of the given dictionary. When the dictionary is updated, the object is also updated.
    • pop(key[, default_value]): Removes the item with the specified keykeyfrom the given dictionary and returns the value of the item with the specified key if such item exists in the dictionary. If no such item with the specified key exists, return the specified default valuedefault_valueif it is given; otherwise, report theKeyErrorerror.
    • popitem(): Removes the last item from the given dictionary and returns the key-value pair of the item as a tuple. If the dictionary is empty, reports theKeyErrorerror.
    • setdefault(key[, value]): Returns the value of the item with the specified keykey.If no such item with the specified key exists, insert the key-value pair as a new item to the dictionary ifvalueis given. If valueis not given, use the default value None.
    • update(iter): Insert the item in the iterableiterwith key-value pairs (as tuples or as dictionary items) to the given dictionary.
    • values(): Returns a view object called dict_valuesthat presents a list that contains all the values of the given dictionary. When the dictionary is updated, the object is also updated.
  • Built-in Functions with Dictionary:
    • all(a_dict): Returns True if all keys of the dictionarya_dictare true or the dictionary is empty; otherwise, returns False.
      • a_dict: Required. A dictionary.
    • any(a_dict): Returns True if any key in the dictionary is true. If the dictionary is empty or no key in the dictionary is true, returns False.
    • enumerate(iter[, start]): Adds a counter that starts withstartto the keys of the dictionaryiter.and returns it as an enumerating object. Ifstartis not given, use 0 to start the counter.
      • iter: Required. A dictionary.
      • start: Optional. A number from which the counter starts. By default, it is 0.
    • len(a_dict): Returns the number of items in the dictionarya_dict.
    • max(a_dict): Return the maximum key in the dictionarya_dict.
    • min(a_dict): Return the minimum key in the dictionarya_dict.
    • sum(iter[, start=0]): Adds all keys of the dictionaryiterandstart and returns the sum. Ifstartis not given, use 0.
      • iter: Required. A dictionary whose keys must be numbers.
      • start: Optional. A number whose value is to be added to the sum of all keys in the dictionary. By default, it is 0.
    • sorted(iter[, key=func_name, reverse=False]): Sorts the dictionaryiterusing its keys with the sorting criteria specified by the functionfunc_nameand returns the sorted dictionary. Ifreverseis True, sort the dictionary in descending order; otherwise, sort the dictionary in ascending order.
      • iter: Required. An iterable to be sorted.
      • key: Optional. A function that specifies the sorting criteria.
      • reverse: Optional. True or False. If it is True, sort the iterable in descending order.