In python, a dictionary is a mixed and unordered collection of data values. The dictionary type stores a key along with its value, where the keys are unique within a collection and the values can hold any arbitrary value. The values in dictionaries are changeable and it does not allow duplicates.
Creating a Dictionary
A Dictionary is created with curly brackets{}, it contains keys and values, to separate each key from its value single colon(:) is used. Each key and value is separated by comma.
Example:
my_dict = {
"Name" : "Ram",
"Department" : "BCA",
"Year" : "3ed"
}
print(my_dict)
print(type(my_dict))
Output:
{'Name': 'Ram', 'Department': 'BCA', 'Year': '3ed'}
<class 'dict'>
We can also create a dictionary using dict() method,
Example:
my_dict = dict([("Name", "Ram"), ("Department" , "BCA"), ("Year" , "3ed")])
print(my_dict)
print(type(my_dict))
Output:
{'Name': 'Ram', 'Department': 'BCA', 'Year': '3ed'}
<class 'dict'>
Access Elements in Dictionary
Dictionaries are indexed using their keys, so we can access a particular value in a dictionary using key inside square brackets.
Example:
my_dict = {"Name" : "Ram", "Department" : "BCA", "Year" : "3ed"}
print(my_dict["Name"], "is from", my_dict["Department"], "department")
Output:
Ram is from BCA department
Updating Dictionary
Dictionaries can be updated directly using the keys or using the update() method.
Example using keys:
my_dict = {"Name" : "Ram", "Department" : "BCA", "Year" : "3ed"}
my_dict["Name"] = "Murali"
print(my_dict)
Output:
{'Name': 'Murali', 'Department': 'BCA', 'Year': '3ed'}
Example using update() method:
my_dict = {"Name" : "Ram", "Department" : "BCA", "Year" : "3ed"}
my_dict.update({"Name" : "Murali"})
print(my_dict)
Output:
{'Name': 'Murali', 'Department': 'BCA', 'Year': '3ed'}
Adding values
We can add a element in a dictionary by dict[key] = value.
Example:
my_dict = {"Name" : "Ram", "Department" : "BCA", "Year" : "3ed"}
my_dict["Semester"] = "5th"
print(my_dict)
Output:
{'Name': 'Ram', 'Department': 'BCA', 'Year': '3ed', 'Semester': '5th'}
Remove Dictionary Elements
There are several methods to remove items from a dictionary. You can either remove individual dictionary elements or clear the entire contents of a dictionary.
del keyword
Using del keyword, we can remove a specific values from a dictionary as well as we can delete whole dictionary.
Example:
#Delete specific value
my_dict = {"Name" : "Ram", "Department" : "BCA", "Year" : "3ed"}
del my_dict["Year"]
print(my_dict)
#Delete entire dictionary
my_dict = {"Name" : "Ram", "Department" : "BCA", "Year" : "3ed"}
del my_dict
print(my_dict)
Output:
Delete specific value
:
{'Name': 'Ram', 'Department': 'BCA'}
Delete entire dictionary
:
Traceback (most recent call last):
File "dictn.py", line 4, in <module>
print(my_dict)
NameError: name 'my_dict' is not defined
Using pop() method
pop() method will remove the value of the key specified.
Example:
my_dict = {"Name" : "Ram", "Department" : "BCA", "Year" : "3ed"}
my_dict.pop("Department")
print(my_dict)
Output:
{'Name': 'Ram', 'Year': '3ed'}
Using popitem() method
popitem() method will remove the lastly inserted item.
Example:
my_dict = {"Name" : "Ram", "Department" : "BCA", "Year" : "3ed"}
my_dict.popitem()
print(my_dict)
Output:
{'Name': 'Ram', 'Department': 'BCA'}
Other Built-in Dictionary methods
get() method
get() method is used access a value for a key.
Example:
my_dict = {"Name" : "Ram", "Department" : "BCA", "Year" : "3ed"}
print(my_dict.get("Name"))
Output:
Ram
keys() method
keys() method will return all the keys in the dictionary.
Output:
my_dict = {"Name" : "Ram", "Department" : "BCA", "Year" : "3ed"}
print(my_dict.keys())
Output:
dict_keys(['Name', 'Department', 'Year'])
values() method
values() method will return all the values in the dictionary.
Output:
my_dict = {"Name" : "Ram", "Department" : "BCA", "Year" : "3ed"}
print(my_dict.values())
Output:
dict_values(['Ram', 'BCA', '3ed'])
items() method
items() method will return all the key and value pairs in the dictionary.
Example:
my_dict = {"Name" : "Ram", "Department" : "BCA", "Year" : "3ed"}
print(my_dict.items())
Output:
dict_items([('Name', 'Ram'), ('Department', 'BCA'), ('Year', '3ed')])
clear() method
clear() method will remove all the items from the dictionary.
Output:
my_dict = {"Name" : "Ram", "Department" : "BCA", "Year" : "3ed"}
my_dict.clear()
print(my_dict)
Output:
{}
Leave a Comment