Dictionary in python:
In dictionary values are enclosed within curly braces, whereas key and item are generally separated by “:” and “,”. If you want the dictionary to be empty then it must be written like this {}.
Here in the dictionary, each value has its own key-value, values can be redundant but keys cannot. Values inside the dictionary can be of any type but keys are immutable just like tuples and strings.
dict1 = {"brand": "bmw","model": "car1","year": 1900}
print(dict1)
How to access the value in Dictionary?
In order to use elements here, square brackets are used with key as its value.
Example:
dict1 = {"brand": "bmw","model": "ravi raj","year": 2000}
xy = dict1["model"]
print(xy)
How to update the value in Dictionary?
You can easily modify or add the values in Dictionary with the help of the Key value pair.
Example:
dict1 = {"brand": "bmw","model": "Ravi raj","year": 1964}
dict1["year"] = 2020
print(dict1)
How to delete the value in the dictionary?
Elements of the dictionary can be easily deleted with the del keyword which can remove the specific value and can delete the entire dictionary.
Example:
#delete the value using del keyword
dict1 = {"brand": "bmw","model": "Krishna","year": 2017}
del dict1["model"]
print(dict1)
#delete the value using pop() function
dict1 = {"brand": "bmw","model": "Krishna","year": 2017}
dict1.pop("model")
print(dict1)
#delete the value using clear() function
dict1 = {"brand": "bmw","model": "Krishna","year": 3056}
dict1.clear()
print(dict1)
