Dictionary
Dictionary
Syntax:
d = {
<key>: <value>,
<key>: <value>,
.
.
.
<key>: <value>
}
More than one entry per key is not allowed (no duplicate key is
allowed)
The values in the dictionary can be of any type, while the keys must
be immutable like numbers, tuples, or strings.
Dictionary keys are case sensitive- Same key name but with the
different cases are treated as different keys in Python dictionaries.
Creating a Dictionary
d = {
"roll": 10,
"name": "Ramesh",
"address": "ctc"
}
print(d)
Output:
Accessing Elements
You can access the items of a dictionary by referring to its key name,
inside square brackets:
d = {
"roll": 10,
"name": "Ramesh",
"address": "ctc"
}
Output:
Characteristics of Dictionary
print(stud)
Output:
Adding Element
Syntax:
Variable[key] = value
d = {}
d[1] = 100
d[2] = 300
print(d)
Removing Element
Syntax:
del variable[key]
d = {}
d[1] = 100
d[2] = 300
# Before Deletion
print(d)
del d[1]
# After Deletion
print(d)
Syntax:
Var.pop(key)
<Key> in <dict_variable>
Returns true if key is present
<Key> not in <dict_variable>
Returns true if key is not present
Dictionary Methods
length()
clear()
get()
items()
keys()
values()
update()