डिप्लोमा इन ऑफिस मैनेजमेंट एण्ड अकाउटिंग

डिप्लोमा इन ऑफिस मैनेजमेंट एण्ड अकाउटिंग

Full Stack Web Development with Laravel

Full Stack Web Development with Laravel

Affiliate Program

Affiliate Program

Python Dictionary

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={
"name":"ajay",
"age":20
}
p.pop("age")
print(p)
popitem() Remove last item from a dictionary
p={
"name":"ajay",
"age":20
}
p.popitem()
print(p)
clear() Remove all items from a dictionary
p={
"name":"ajay",
"age":20
}
p.clear()
print(p)
get() Get specified key item
p={
"name":"ajay",
"age":20
}
print( p.get("age") )
keys() Return list of all dictionary keys
p={
"name":"ajay",
"age":20
}
x = p.keys()
print(x)
values() Return list of all dictionary values
p={
"name":"ajay",
"age":20
}
x = p.values()
print(x)
items() Return list of all dictionary items
p={
"name":"ajay",
"age":20
}
x = p.items()
print(x)
© 2016 - 2023, All Rights are Reserved.