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.
d = {
"roll": 10,
"name": "Ramesh",
"address": "ctc"
}
print(d)
Output:
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:
print(stud)
Output:
Syntax:
Variable[key] = value
d = {}
d[1] = 100
d[2] = 300
print(d)
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
length()
clear()
get()
items()
keys()
values()
update()