Python | Dictionary Methods

Python Dictionary is an unordered collection of key:value pairs.In key:value pair key have to be unique and immutable while value can be anything. For example – { 1 : “Computer”, 2 : “Science”, 3 : “Hub” } is a Python Dictionary.
Below is a table containing all Python Dictionary Methods with description for each one.

Python Dictionary Methods

keys()Returns a dict_keys object containing list of all keys in dictionary
values()Returns a dict_values object containing list of all values in dictionary
items()Returns a dict_items object containing a list of tuples of key, value pairs
copy()Creates a new copy of dictionary
clear()Clears out all key:value pairs from dictionary and change it to an empty dictionary
update()Adding new key:value pair into dictionary
get()Returns value of specified key, if key is not found inside Dictionary then returns none
pop()Removes key:value pair from dictionary for a specified key. If in case key is not in dictionary then raise KeyError
setdefault(key, optional value)Returns value of key, if key is in dictionary. Otherwise insert the key and assign it "optional value"
popitem()Removes any key:value pair from dictionary
len(Dictionary)Returns number of keys inside Dictionary
del Dictionary[key]Deletes key:value pair corresponding specified key from Dictionary
Dictionary[key]Returns value corresponding to specified key, if it exists in Dictionary. But if specified key is not in Dictionary then returns a KeyError
fromkeys(first dictionary, specified value)This method returns a new dictionary having key:value pairs as keys from first dictionary and value as specified value

Moreover about Python Dictionaries –

Comments to: Python | Dictionary Methods

Your email address will not be published.