Let's see in detail about the built in method in Dictionary and how to use it.
items()
Items() method can be used to find the element inside the dictionary.
d={"name":"php","age":20}
print(d.items())
''''
output:
dict_items([('name', 'php'), ('age', 20)])
''''
Description:-
print(d.items()) -> Prints the item in dictionary d.
But look at the format printed on the output screen.
() -> round brackets represent a tuple.
[] -> square bracket represents list.
Here the key and value are given inside the tuple. Since there are two items in total, two tuples are formed. These two tuples are also given inside a list.
keys()
The keys() method is used to retrieve the key from the dictionary.
d={"name":"php","age":20}
print(d.keys())
''''
output:
dict_keys(['name', 'age'])
''''
Description:-
d.keys() -> This statement takes only the name and age keys in the dictionary. Therefore, we can use the keys method to find out which keys are given. It displays the fetched key as a list data type. .
values()
The method values() is used to get only the value of the element inside the dictionary.
d={"name":"php","age":20}
print(d.values())
''''
output:
dict_values(['php', 20])
''''
Description:-
d.values() -> takes only php and 20 value inside the dictionary called d.
get()
Get() can be used to retrieve the value of a particular key.
d={"name":"php","age":20}
print(d.get("name"))
''''
output:
php
''''
Description:-
d.get(“name”) -> This statement displays the value of php with key “name”.
d={"name":"php","age":20}
print(d.get("address","not found"))
''''
output:
not found
''''
Description:-
d.get(“address”, “not found”) -> In this statement, we will see if the key address is in the dictionary. If not, the text “not found” given next to “address” will be displayed.
d={"name":"php","age":20}
print(d.get("name","not found"))
''''
output:
php
''''
Description:-
php displays the value because there is a key called Name.
update()
A specific item should be added to the dictionary along with key and value using update method.
d={"name":"php","age":20}
d.update(address="nill")
print(d)
''''
output:
{'name': 'php', 'age': 20, 'address': 'nill'}
''''
Description:-
d.update(address = ‘nil’) -> where address is the key. nil is the value. The address item can be added to the dictionary using the update method.
When printing, the address:nil element is added.
d1={"name":"php","age":20}
d2={"address":"nill"}
d1.update(d2)
print(d1)
''''
Output:
{'name': 'php', 'age': 20, 'address': 'nill'}
''''
Description:-
The dictionary named d1 has two items named name and age.
There is an item called address in the dictionary called d2.
d1.update(d2)-> This statement changes the item in dictionary D2 together with d1 into a single dictionary.
Print(d1) -> d1 displays three items in the output.
copy()
Copy() method can be used to copy all the elements in the dictionary.
d1={"name":"php","age":20}
d2=d1.copy()
print(d2)
''''
Output:
{'name': 'php', 'age': 20}
''''
Description:-
d2=d1.copy()-> copy() method copies the dictionary d1 and stores it in the variable d2.
print(d2) -> When you print the variable called d2, all the values in D2 are in d2.
pop()
Using the pop() method removes the element in the dictionary based on the key.
d={"name":"php","age":20}
d.pop("name")
print(d)
''''
output:
{'age': 20}
''''
Description:-
d.pop('name') -> In this statement pop('name') method removes the key named name from the dictionary.
popitem( )
The popitem( ) method is used to remove the last element in the dictionary.
d={"name":"php","age":20}
d.popitem()
print(d)
''''
Output:
{'name': 'php'}
''''
Description:-
d.popitem() -> This statement removes the last element in the dictionary, age, from the dictionary.
clear()
The clear() method is used to delete all the elements in the dictionary.
d={"name":"php","age":20}
d.clear()
print(d)
''''
Output:
{}
''''
Description:-
d.clear() -> This statement removes all the elements inside the Dictionary and gives us an empty dictionary in the output.