Dictionary Methods

clear(): Removes all the items from dictionary.

SYNTAX:

dict.clear()

Example:

car = {"Company":"Mahindra", "Model":"XUV300", "Price":795000}
print(car)
car.clear()
print(car)

Output:

{'Company': 'Mahindra', 'Model': 'XUV300', 'Price': 795000}
{}

copy(): Returns a copy of dictionary.

SYNTAX:

dict.copy()

Example:

car = {"Company":"Mahindra", "Model":"XUV300", "Price":795000}
print(car)
dupcar = car.copy()
print(dupcar)

Output:

{'Company': 'Mahindra', 'Model': 'XUV300', 'Price': 795000}
{'Company': 'Mahindra', 'Model': 'XUV300', 'Price': 795000}

fromkeys(): Creates a new dictionary with keys from sequence and values set to value.

SYNTAX:

dict.fromkeys(sequence[,value])

Example1:

details = ["Company", "Model", "Price"]
car = dict.fromkeys(details)
print(car)

Output1:

{'Company': None, 'Model': None, 'Price': None}

Example2:

details = ["Company", "Model", "Price"]
car = dict.fromkeys(details,"Mahindra")
print(car)

Output2:

{'Company': 'Mahindra', 'Model': 'Mahindra', 'Price': 'Mahindra'}

get(): Returns a value from the given key. If the key is not avaliable then returns default value None.

SYNTAX:

dict.get(key, default=None)

Example1:

car = {"Company":"Mahindra", "Model":"XUV300", "Price":795000}
print(car.get("Company"))

Output1:

Mahindra

Example2:

car = {"Company":"Mahindra", "Model":"XUV300", "Price":795000}
print(car.get("KMPL"))
print(car.get("KMPL", 15))

Output2:

None
15

items(): Returns a list of dictionary (key, value) tuple pairs.

SYNTAX:

dict.items()

Example:

car = {"Company":"Mahindra", "Model":"XUV300", "Price":795000}
print(car.items())

Output:

dict_items([('Company', 'Mahindra'), ('Model', 'XUV300'), ('Price', 795000)])

keys(): Returns list of keys avaliable in dictionary.

SYNTAX:

dict.keys()

Example:

car = {"Company":"Mahindra", "Model":"XUV300", "Price":795000}
print(car.keys())

Output:

dict_keys(['Company', 'Model', 'Price'])

pop(): Removes an element from the dictionary with the specified key and returns the deleted value. If key is not in dictionary defaultvalue is the deleted element.

SYNTAX:

dict.pop(key, defaultvalue)

Example1:

car = {"Company":"Mahindra", "Model":"XUV300", "Price":795000}
print(car.pop("Price"))
print(car)

Output1:

795000
{'Company': 'Mahindra', 'Model': 'XUV300'}

Example2:

car = {"Company":"Mahindra", "Model":"XUV300", "Price":795000}
print(car.pop("KMPL", 15))
print(car)

Output2:

15
{'Company': 'Mahindra', 'Model': 'XUV300', 'Price': 795000}

popitem(): Removes the last item inserted into dictionary and returns the deleted item.

SYNTAX:

dict.popitem()

Example:

car = {"Company":"Mahindra", "Model":"XUV300", "Price":795000}
print(car.popitem())
print(car)

Output:

('Price', 795000)
{'Company': 'Mahindra', 'Model': 'XUV300'}

setdefault(): This method is similar to get(), but will set dict[key]=default if the key is not already in dict.

SYNTAX:

dict.setdefault(key, default=None)

Example:

car = {"Company":"Mahindra", "Model":"XUV300", "Price":795000}
car.setdefault("Price",975000)
print(car)
car.setdefault("KMPL", 15)
print(car)

Output:

{'Company': 'Mahindra', 'Model': 'XUV300', 'Price': 795000}
{'Company': 'Mahindra', 'Model': 'XUV300', 'Price': 795000, 'KMPL': 15}

update(): Updates a dictionary with another dictionary's key-value pairs.

SYNTAX:

dict.update(dict2)

Example:

carInfo = {"Company":"Mahindra", "Model":"XUV300"}
carInfo2 = {"Price":795000, "KMPL":15}
carInfo.update(carInfo2)
print(carInfo)

Output:

{'Company': 'Mahindra', 'Model': 'XUV300', 'Price': 795000, 'KMPL': 15}

values(): Returns a list of values avaliable in the dictionary.

SYNTAX:

dict.values()

Example:

car = {"Company":"Mahindra", "Model":"XUV300", "Price":795000}
print(car.values())

Output:

dict_values(['Mahindra', 'XUV300', 795000])