Dictionary

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.

Related Courses

Example

The example below creates a dictionary. Keys must be unique values, you can not use the same key twice. Values may or may not be unique.

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’. 

English
Note: A dictionary has no specific order.

Add and remove

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

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

Previous Post
Next Post

Leave a Reply