
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Dictionary Methods in Python Program
A python dictionary is a collection data type which is wrapped in braces, {}, with a series of key value pairs inside the braces. Each key is connected to a value. We use a key to access the value associated with that key. A key can be a number, a string, a list, or even another dictionary.
Dictionary Methods
There are many in-built methods available in the python Standard Library which is useful in dictionary operations. Below we will see the examples of most frequently used dictionary methods.
keys()
The method keys() returns a list of all the available keys in the dictionary.
Example
dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97} print(dict.keys())
Running the above code gives us the following result
Output
dict_keys(['Name', 'Rollno', 'Dept', 'Marks'])
items()
This method returns a list of dictionary's (key, value) as tuple.
Example
dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97} print(dict.items())
Running the above code gives us the following result
Output
dict_items([('Name', 'Harry'), ('Rollno', 30), ('Dept', 'cse'), ('Marks', 97)])
values()
This method returns list of dictionary dictionary's values from the key value pairs.
Example
dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97} print(dict.values())
Running the above code gives us the following result:
Output
dict_values(['Harry', 30, 'cse', 97])
pop()
The method pop(key) Removes and returns the value of specified key.
Example
dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97} dict.pop('Marks') print(dict)
Running the above code gives us the following result:
Output
{'Name': 'Harry', 'Rollno': 30, 'Dept': 'cse'}
copy()
This method Returns a shallow copy of dictionary.
Example
dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97} dict_new=dict.copy() print(dict_new)
Running the above code gives us the following result:
Output
{'Name': 'Harry', 'Rollno': 30, 'Dept': 'cse', 'Marks': 97}
clear()
The method clear() Removes all elements of dictionary.
Example
dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97} dict.clear() print(dict)
Running the above code gives us the following result:
Output
{}
get()
This method returns value of given key or None as default if key is not in the dictionary.
Example
dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97} print('\nName: ', dict.get('Name')) print('\nAge: ', dict.get('Age'))
Running the above code gives us the following result:
Output
Name: Harry Age: None
update()
The update() inserts new item to the dictionary.
Example
dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97} dict.update({'Age':22}) print(dict)
Running the above code gives us the following result
Output
{'Name': 'Harry', 'Rollno': 30, 'Dept': 'cse', 'Marks': 97, 'Age': 22}
- Related Articles
- Dictionary Methods in Python
- Built-in Dictionary Functions & Methods in Python
- Dictionary Methods in Python (update(), has_key(), fromkeys()
- Dictionary Methods in Python (cmp(), len(), items()…)
- Dictionary Methods in C#
- Dictionary Methods in Java
- Extract Unique dictionary values in Python Program
- Python Program To Create A Dictionary With A Dictionary Literal
- Python Program – Create dictionary from the list
- Python Program to Search an Element in a Dictionary
- Convert string dictionary to dictionary in Python
- Python program to find the second maximum value in Dictionary
- Python Program to Multiply All the Items in a Dictionary
- Python Program to print key value pairs in a dictionary
- Python Program To Find The Largest Element In A Dictionary
