Python Dictionary

Dictionary

Dictionary is used to store multiple values in key-value pair format.

Dictionary is created using curly brackets { }.

Dictionary is a mutable data type, which means it can be edited after creation.

Ordered and Changeable

Dictionaries are ordered, meaning the items have a defined sequence that will not change.

Dictionary elements are in key:value format. Values can be accessed and changed using keys.

Duplicates are Not Allowed

A dictionary cannot have two items with the same key. However, items with the same values are allowed.

Example

Example of a Python Dictionary

Output

{'one': 1, 'two': 2, 'three': 3}

{'one': 1, 'two': 1, 'three': 3}


Dictionary Length

The length of a dictionary is the number of elements it contains, and it can be determined using the len() function.

Example

Example of finding length of a Python Dictionary

Output

3



The dict() Constructor

The dict() constructor can be used to create a dictionary from key-value pairs.

Example

Example of using dict() constructor in Python

Output

{'name': 'John', 'age': 36, 'country': 'Norway'}