Python Dictionary
Python Dictionary is the collection of multiple data like the list and tuple. The main difference between the list and the dictionary is that the dictionary elements are unordered. Whereas, dictionary elements always have a key:value pair.
Example to create and declare python dictionary
my_dictionary = {
"Title": "TutorialsClass",
"URL": "https://tutorialsclass.com/",
"Year": 2019
}
print(my_dictionary )
{‘Title’: ‘TutorialsClass’, ‘URL’: ‘https://tutorialsclass.com/’, ‘Year’: 2019}
Creating dictionary using the dict keyword
We can also create the dictionary by using a dict
keyword.
Example of creating a dictionary using the dict keyword
my_dictionary =dict(
Title = "TutorialsClass",
URL = "https://tutorialsclass.com/",
Year = 2019
)
print(my_dictionary)
{‘Title’: ‘TutorialsClass’, ‘URL’: ‘https://tutorialsclass.com/’, ‘Year’: 2019}
Printing a dictionary
Printing a dictionary is simple like printing a variable.
Example to print dictionary
my_dictionary = {
"Title": "TutorialsClass",
"URL": "https://tutorialsclass.com/",
"Year": 2019
}
print(my_dictionary)
{‘Title’: ‘TutorialsClass’, ‘URL’: ‘https://tutorialsclass.com/’, ‘Year’: 2019}
Topics to remember about Python Dictionary
Dictionary in Python are ordered and have a definite count. Also, we can concatenate the sets.
Printing a specific element from the Dictionary
We can print the specific value from the dictionary by specifying a key.
Example to print a specific element of dictionary
my_dictionary = {
"Title": "TutorialsClass",
"URL": "https://tutorialsclass.com/",
"Year": 2019
}
print(my_dictionary["Year"])
2019
Example to print a specific element of dictionary using the get() function
my_dictionary = {
"Title": "TutorialsClass",
"URL": "https://tutorialsclass.com/",
"Year": 2019
}
print(my_dictionary.get("Year"))
2019
Python Dictionary – Modyfing or updating elements:
We can modify elements from the dictionary directly.
Example to update the elements of dictionary
my_dictionary = {
"Title": "TutorialsClass",
"URL": "https://tutorialsclass.com/",
"Year": 2019
}
my_dictionary ["Year"] = 2018
print(my_dictionary )
{‘Title’: ‘TutorialsClass’, ‘URL’: ‘https://tutorialsclass.com/’, ‘Year’: 2018}
Adding elements to a dictionary
We can add any number of elements in a dictionary directly without using some inbuilt functions.
Example to add new element to dictionary
my_dictionary = {
"Title": "TutorialsClass",
"URL": "https://tutorialsclass.com/",
"Year": 2019
}
my_dictionary ["Birth"] = 2018
print(my_dictionary )
{‘Title’: ‘TutorialsClass’, ‘URL’: ‘https://tutorialsclass.com/’, ‘Year’: 2019, ‘Birth’: 2018}
Copying one dictionary to the other dictionary
We can easily copy one dictionary to the other by using the copy()
method.
Example to copy one dictionary to other dictionary
my_dictionary_1 = {
"Title": "TutorialsClass",
"URL": "https://tutorialsclass.com/",
"Year": 2019
}
my_dictionary_2 = my_dictionary_1.copy()
print(my_dictionary_2)
{‘Title’: ‘TutorialsClass’, ‘URL’: ‘https://tutorialsclass.com/’, ‘Year’: 2019}
Checking length of a Python dictionary
With the help of the len() function we can check how many numbers of key:value pair exist in the dictionary.
Example to calculate the length of dictionary
my_dictionary = {
"Title": "TutorialsClass",
"URL": "https://tutorialsclass.com/",
"Year": 2019
}
print(len(my_dictionary ))
3
Description of Output
Here you can see the output is 3, which means there are three key:value pairs in our this Dictionary.
Python Dictionary- Removing the specific element from the dictionary
We can remove the value in many ways.
Example removing a specific element from the user With the help of pop() function
my_dictionary = {
"Title": "TutorialsClass",
"URL": "https://tutorialsclass.com/",
"Year": 2019
}
my_dictionary .pop("Year")
print(my_dictionary )
{‘Title’: ‘TutorialsClass’, ‘URL’: ‘https://tutorialsclass.com/’}
Example to remove a specific element from the user With the help del keyword
my_dictionary = {
"Title": "TutorialsClass",
"URL": "https://tutorialsclass.com/",
"Year": 2019
}
del my_dictionary["Year"]
print(my_dictionary )
{‘Title’: ‘TutorialsClass’, ‘URL’: ‘https://tutorialsclass.com/’}
Example to remove a specific element from the user With the help popitem() function
my_dictionary = {
"Title": "TutorialsClass",
"URL": "https://tutorialsclass.com/",
"Year": 2019
}
my_dictionary .popitem( )
print(my_dictionary )
{‘Title’: ‘TutorialsClass’, ‘URL’: ‘https://tutorialsclass.com/’}
Removing all elements from the dictionary
We can remove all the key:value pairs with the help of clear() function, we can remove all the elements from the dictionary.
Example to remove all the elements from dictionary
my_dictionary = {
"Title": "TutorialsClass",
"URL": "https://tutorialsclass.com/",
"Year": 2019
}
my_dictionary .clear()
print(my_dictionary )
{}
Description of Output
Here you can see that all the items from the dictionary have been cleared.
Python Dictionary – Delete the dictionary
In Python programming, we can delete the whole dictionary as well using the del
keyword
my_dictionary ={
"Title": "TutorialsClass",
"URL": "https://tutorialsclass.com/",
"Year": 2019
}
del my_dictionary
Description of Output
Here you can see that the output is blank because we have deleted the dictionary. So, it will not return anything.
Checking if any item exists in Python Dictionary
Like a list, we can easily check if an element is present in the dictionary or not.
Example to an element is exists in python dictionary or not
my_dictionary = {
"Title": "TutorialsClass",
"URL": "https://tutorialsclass.com/",
"Year": 2019
}
if "Year" in my_dictionary :
print("Yes, 'Year' is in this dictionary")
else:
print("No, 'Year' is not in this dictionary")
Yes, ‘Year’ is in this dictionary
Important Points about Python Dictionary
- Keys in Dictionary can’t be duplicated.
- We can also use for loop for printing the dictionary.
Example to print the keys and value by using for loop
my_dictionary = {
"Title": "TutorialsClass",
"URL": "https://tutorialsclass.com/",
"Year": 2019
}
# Printing all key names in the dictionary one by one
for x in my_dictionary:
print(x)
# Printing all the values in the dictionary one by one
for x in my_dictionary:
print(my_dictionary[x])
# Another way to print all the values in the dictionary one by one
for x in my_dictionary.values():
print(x)
# printing both the values and keys
for x, y in my_dictionary.items():
print(x, y)
Title
URL
Year
TutorialsClass
2019
TutorialsClass
2019
Title TutorialsClass
URL https://tutorialsclass.com/
Year 2019
Exercises & Assignments |
---|
No Content Found. |
Interview Questions & Answers |
---|
No Content Found. |