len()
This method is used to return the length of the dictionary. That is, the number of items (key-value pairs)
Example:
dict1={'name':'john','course':'B.Tech','Stipen':9000} print(len(dict1))
Output:
3
clear()
This method to delete all entries in the dictionary.
Example:
dict1={'name':'john','course':'B.Tech','Stipen':9000} dict1.clear() print(dict1)
Output:
{}
get()
This method is used to return the value for the key passed as an argument. If the key is not present in the dictionary, it will return the default value. If no default value is specified, then it will return none.
Example:
dict1={'name':'john','course':'B.Tech','Stipen':9000} print(dict1.get("name"))
Output:
John
items()
This method is used to return a list of tuples(key-value pair).
Example:
dict1={'color1':'red','color2':'green','color3':'blue'} print(dict1.items())
Output:
dict_items([('color1', 'red'), ('color2', 'green'), ('color3', 'blue')])
keys()
This method is used to return a list of keys in the dictionary.
Example:
dict1={'name':'smith','country':'usa','salary':908780} print(dict1.keys())
Output:
dict_keys(['name', 'country', 'salary'])
values()
This method is used to return a list of values in the dictionary.
Example:
dict1={'name':'smith','country':'USA','salary':908780} print(dict1.values())
Output:
dict_values(['smith', 'USA', 908780])
Leave a comment