Dictionaries in Python
Dictionaries in Python
A Python dictionary is a data structure that stores the value in key: value pairs. Values in a dictionary
can be of any data type and can be duplicated, whereas keys can’t be repeated and must be
immutable.
The data is stored in key:value pairs in dictionaries, which makes it easier to find values.
In Python, a dictionary can be created by placing a sequence of elements within curly {} braces,
separated by a ‘comma’.
print(d2)
print(d3)
Dictionary keys are case sensitive: the same name but different cases of Key will be treated
distinctly.
Keys must be immutable: This means keys can be strings, numbers, or tuples but not lists.
Keys must be unique: Duplicate keys are not allowed and any duplicate key will overwrite
the previous value.
Dictionary internally uses Hashing. Hence, operations like search, insert, delete can be
performed in Constant Time.
# example
d = { "name": "Alice", 1: "Python", (1, 2): [1,2,4] }
del d2[“2"]
print(d2)
print(d2)
val = d1.pop(4)
print(val)
# Example Using popitem to removes and returns the last key-value pair.
print(d) # output {1: 'Geeks', 2: 'For', 3: {'A': 'Welcome', 'B': 'To', 'C': 'Geeks'}}