Dictionary in Python

Values are unordered pair of key-value pairs

Each value has a corresponding key. Thus there exists a one-to-one relation between them

You cannot get a key by value, as otherwise

Keys are immutable but values can either be immutable or mutable

Dictionary cannot have a duplicate key thus a one-to-one relation must always exist between key and value. Assigning a value to an existing key will wipe out the old value

>>> d = {“Hello”:10, “world”:20}

>>> d

{‘world’: 20, ‘Hello’: 10}

>>> d[“Hello”]


>>> d[“Hello”] = 30

>>> d

{‘world’: 20, ‘Hello’: 30}


  • Dictionaries support the following in-built methods, but are not limited to these:

d[k] retrieves a value from a dictionary

d[k]=x sets a value in the dictionary

d.has_key(k) tests for the presence of a keyword

d.get(k, d) returns a default if a key is not found

d.keys() returns a list of keys from a dictionary

d.values() returns a list of values

Leave a Reply

Your email address will not be published. Required fields are marked *