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.

Table of Contents

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 –

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts