Python dictionaries are used to store items in the form of key value pairs.
Note: Dictionaries are mutable and does not allow duplicate values.
person={
"name" : "Ajay",
"age" : 20,
"country" : "India"
}
Access dictionary
print(person)
Access dictionary items
print(person['name'])
Change items in dictionary
person={
"name" : "Ajay",
"age" : 20,
"country" : "India"
}
person["name"]="Rakesh"
print( person )
Add new item in dictionary
person["email"]="Rakesh@gmail.com"
Remove specific item from a dictionary
person={
"name" : "Ajay",
"age" : 20,
"country" : "India"
}
person.pop("name")
Dictionary methods
pop(item) | Remove specified item from a dictionary |
p={ |
popitem() | Remove last item from a dictionary |
p={ |
clear() | Remove all items from a dictionary |
p={ |
get() | Get specified key item |
p={ |
keys() | Return list of all dictionary keys |
p={ |
values() | Return list of all dictionary values |
p={ |
items() | Return list of all dictionary items |
p={ |