Python Dictionary Method's
These are all the built-in methods that you can use to manipulate the python dictionary.
|
Python Dictionary Method's and python built-in function |
clear() function in python:-
clear() method in python dictionary use to remove all elements from a dictionary.
Syntax : -
dictionary.clear()
copy() method in python dictionary:-
As its name says copy() method gives a copy of the specified dictionary.
Syntax : -
dictionary.copy()
fromkeys() Method : -
formkeys() method helps to return a python dictionary with specified keys and the specified value.
Syntax : -
dict.fromkeys(keys, value)
get() Method : -
get() method helps to return the value of the item with the specified key.
Syntax : -
dictionary.get(keyname, value)
items() Method : -
items() in python dictionary gives the view object that contains key-value pairs of the dictionary, and reflect any changes made to the dictionary
Syntax : -
dictionary.items()
key() Method : -
key() in python dictionary returns the view object that contains the key of the dictionary as a list and reflects any changes made to the dictionary.
Syntax : -
dictionary.keys()
pop() Method in python: -
pop() in python follows the same method like remove() function the only difference is pop() function remove the specified item from the dictionary.
Syntax : -
dictionary.pop(keyname, defaultvalue)
remove item will be the return value of pop() function.
popitem() Method in python: -
popitem() in python dictionary removes the last inserted item into the dictionary.
Syntax : -
dictionary.popitem()
removed value in popitem() will be the return value in a python dictionary.
Python Dictionary setdefault() Method
setdefault() method gives/returns the value from python dictionary the item with the specified key. If any key does not exist, insert the key, with the specified value.
Syntax : -
dictionary.setdefault(keyname, value)
Python Dictionary update() Method:-
update() method use to inserts the specified items to the python dictionary.
Syntax : -
dictionary.update(iterable)
values() Method in python dictionary:-
values() method used to return the view object that contains the values of the dictionary, as a list.
Syntax : -
dictionary.values()
Reflect any changes make to the python dictionary
0 comments:
Post a Comment