Online Courses

Dictionary example
A dictionary in Python is a one to one mapping. Every key points to a value, separated by a colon (:). A dictionary is defined using curly brackets. The value left of the colon is called the key, the value right of the colon is called the value. Every (key,value) pair is separated by a comma.

Keys must be unique values, you can not use the same key twice. Values may or may not be unique.


k = { 'EN':'English', 'FR':'French' }

We defined a dictionary named k and access elements using the square brackets you’ve seen before. We use the key [‘EN’] to print the value ‘English’.  A dictionary has no specific order.

Left: the keys ‘EN’ and ‘FR’, right: the values ‘English’ and ‘French’. Thus the dictionary contains the mappings: (‘EN’,’English’), (‘FR’,’French’).

To add a new value to a dictionary you can simply assign a key value pair:

k['DE'] = 'German'

To remove a key/value pair use the del keyword:

k = { 'EN':'English', 'FR':'French' }

del k['FR']

Read and Write File

Leave a Reply

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