
- 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 (update(), has_key(), fromkeys()
Dictionary in python is one of the most frequently used collection data type. It is represented by hey value pairs. Keys are indexed but values may not be. There are many python-built in functions that make using the dictionary very easy in various python programs. In this topic we will see the three in-built methods namely update(), has_key() and fromkeys().
update()
The method update adds new items to a given dictionary by merging the items from the secondary with first.
Syntax
dict1.update(dict2) Where dict1 and dict2 are the two input dictionaries.
In the below example we see pairs of dictionaries. The second dictionary gets added to the items in the first dictionary. But the name of keys should be different in the second dictionary to see the effect of merging.
Example
dict1 = {'Place': 'Delhi', 'distance': 137}; dict2 = {'Temp': 41 }; dict1.update(dict2) print(dict1)
Running the above code gives us the following result −
{'Place': 'Delhi', 'distance': 137, 'Temp': 41}
has_key()
This method verifies of a key is present in a dictionary or not. This is python2 only feature. This method is not available in python3.
Syntax
dict.has_key(key)
In the below example we check for the presence of some keys in the given dictionaries.
Example
dict1 = {'Place': 'Delhi', 'distance': 137}; dict2 = {'Temp': 41 }; print(dict1.has_key('Place')) print(dict2.has_key('Place'))
Running the above code gives us the following result −
Output
True False
dict.fromkeys(seq[, value]))
In this method we convert a sequence of values to a dictionary. We can also specify a value which become spart of every key.
Syntax
dict.fromkeys(seq)
In the below example we create a dictionary out of a sequence and add a value to it..
Example
seq = {'Distnace','Temp','Humidity'} dict = dict.fromkeys(seq) print(dict) dict = dict.fromkeys(seq,15) print(dict)
Running the above code gives us the following result −
Output
{'Distnace': None, 'Humidity': None, 'Temp': None} {'Distnace': 15, 'Humidity': 15, 'Temp': 15}
- Related Articles
- Python - Dictionary has_key()
- Dictionary Methods in Python
- Dictionary Methods in Python Program
- Built-in Dictionary Functions & Methods in Python
- Dictionary Methods in Python (cmp(), len(), items()…)
- How to update a Python dictionary values?
- What is usage of update method in Python dictionary?
- Dictionary Methods in C#
- Dictionary Methods in Java
- How to update the value of a key in a dictionary in Python?
- How to update the value stored in a Dictionary in C#?
- Convert string dictionary to dictionary in Python
- C++ Program to Update value of Dictionary using key
- Updating Dictionary in Python
- Dictionary Data Type in Python
