In the previous article we have looked at the definition, structure and various ways to add or remove different aspects of the Dictionary data type. In this article, we will continue to explore more about the Dictionary data type in Python along with operations such as len( ), copy( ) ,items ( ), fromkeys ( ), setdefault ( ) and keys ( ).
Table of Contents
Working with Dictionaries in Python
Using this method, you can count the number of elements in the dictionary. For example:
Books_collection = {
"Book": "Ulysses",
"Author": "James Joyce",
"year": 1920
}
print(len(Books_collection))
Output:
3
This dictionary has three entries, so the method will return 3.
Copy () method Python Dictionaries
This method returns a copy of the existing dictionary. For example:
Books_collection = {
"Name": "Adam",
"Country": "Indian",
"Year Born": 1980,
"Hobbies": "Reading",
}
x = Books_collection .copy()
print(x)
Output:
{'Name': 'Adam', 'Country': 'Indian', 'Year Born': 1980, 'Hobbies': 'Reading'}
A copy of the dictionary has been created dictionary_sample
. It is assigned to a variable x
. If you output x
to the console, then it will contain the same elements as in the dictionary dictionary_sample
.This is convenient because changes to the copied dictionary do not affect the original dictionary.
items( ) method Python Dictionaries
This method returns an iterable object. Such an object contains key-value pairs for a dictionary, similar to tuples in a list. The method is used when you need to iterate over the values of the dictionary. This method must be called along with the dictionary, as in the example below:
Books_collection = {
"Name": "Adam",
"Country": "Indian",
"Year Born": 1980,
"Hobbies": "Reading",
}
for k, v in Books_collection.items(): print(k, v)
Output:
Name Adam
Country Indian
Year Born 1980
Hobbies Reading
Fromkeys( ) method Python Dictionaries
This method returns a dictionary with the specified keys and values. The value of the required parameter keys is iterable objects. It is responsible for the keys of the new dictionary. The value for the parameter is value optional. It is responsible for the default value for all keys. The default is None. Suppose you want to create a dictionary with three keys and the same value. This can be done as follows:
name = ('James', 'Peter', 'Elizabeth')
age = 45
dict_sample = dict.fromkeys(name, age)
print(dict_sample)
Output:
{'James': 45, 'Peter': 45, 'Elizabeth': 45}
The code above defines keys and one value. The method fromkeys( ) iterates over the keys and combines them with the value to create a complete dictionary.
Setdefault( ) method Python Dictionaries
This method is used when you need to get the value of an element with a specific key. If the key is not found, it will be inserted into the dictionary along with the specified value. The parameter keyname is required in this function . It denotes the name of the key whose value you want to return. The parameter is value optional. If there is already a key in the dictionary, the parameter will have no effect. If the key does not exist, then the function value becomes the key value. The default is None
.
sample = {
"Company": "Volkswagen",
"model": "Polo",
"year": 2018
}
x = sample.setdefault("color", "Gray")
print(x)
Output:
Gray
There is no key in the dictionary color
. The method setdefault( ) inserts this key along with the value “Gray”. The following example shows how the method works if such a key already exists:
sample = {
"Company": "Volkswagen",
"model": "Polo",
"year": 2018,
"color": "Black"
}
x = sample.setdefault("color", "Gray")
print(x)
Output:
Black
The value “Black” did not affect the dictionary because the key already has a value.
Keys () method Python Dictionaries
This method also returns an iterable object. It is a list of all the keys in the dictionary. Like the method items()
, this one displays changes in the dictionary itself. This method is often used to iterate over all the keys in a dictionary:
To use the method, you just need to use it with the name of the dictionary, as shown below:
sample = {
"Company": "Volkswagen",
"model": "Polo",
"year": 2018
}
x = sample.keys( )
print(x)
Output:
dict_keys(['brand', 'model', 'year'])