ScanSkill

dictionary

The dictionary or dict is used to store key-value pairs in one variable.

Syntax

dictionary_name = {"key1": value1, "key2": value2}
  • Adding new key-pair to the dictionary
dictionary_name["key3"] = value3
  • Accessing items from the dictionary
dictionary_name["key1"]

Note:

  • Dictionaries come with helper functions that list keys and values, as well as check the existence of keys in a dictionary (key in the dictionary).

Example

>>> days_of_week = {'Monday': 'mon', 'Tuesday': 'tues'}

>>> # Add a new key-value pair
>>> days_of_week['Wednesday'] = 'wed'
>>> days_of_week
{'Monday': 'mon', 'Tuesday': 'tues', 'Wednesday': 'wed'}


>>> print (days_of_week['Monday']) 
'mon'