Python dictionary

In Python, a dictionary is a built-in data structure that stores data in key-value pairs. Dictionaries are mutable, meaning you can change, add, or remove items. Each key in a dictionary must be unique, immutable, and hashable (e.g., strings, numbers, or tuples).

Creating a Dictionary

Dictionaries can be created using curly braces {} with pairs of keys and values separated by a colon :.


dictData = {
    "name": "John",
    "age": 35,
    "city": "London"
}

Alternatively, you can create an empty dictionary and add items later:


dictData = {}
dictData ["name"] = "John"
dictData ["age"] = 35
dictData ["city"] = "London"

You can also use the dict() constructor


my_dict = dict(name="John", age=35, city="London")