Dictionaries in Python are a fundamental data structure that stores a collection of key-value pairs. Python dictionaries help to store objects’ unordered dictionaries. We define dictionaries using curly brackets; they are a pair of keys and a value. We can not retrieve a value through its index because dictionaries do not store values in order or sequence.
Code
dic = {'key': 'value1', 'key2': 2}
print(dic)
PythonOutput: {‘key’: ‘value1’, ‘key2’: 2} |
Note: We cannot retrieve a value through its index because dictionaries do not store values in order or sequence. We can retrieve a value by using its key.
Access Item from Dictionaries
Code
dic = {'key1': 'value1', 'key2': 2}
print(dic['key1'])
PythonOutput: value1 |
We can also use get () which will give you the same result:
print (dic.get(key1))
PythonOutput: value1 |
Change Item from Dictionary
We can change any item value from the dictionary by using its key name:
Code
dic = {'key1': 'value1', 'key2': 2}
dic["key2"] = 5
print(dic)
PythonOutput: {‘key1’: ‘value1’, ‘key2’: 5} |
Add new Item
We can change any item value from the dictionary by using its key name:
Code
dic = {'key1': 'value1', 'key2': 2, 'key3': 'value3'}
dic["key3"] = "red"
print(dic)
PythonOutput: {‘key1’: ‘value1’, ‘key2’: 2, ‘key3’: ‘red’} |
Print a List using for loop
We can print our dictionary by using the for loop:
Note: When we print dictionary values through looping only key values will be printed, there are methods to print values as well.
Code
dic = {'key1': 'value1', 'key2': 2}
for key in dic:
print(dic[key])
PythonOutput: value1 2 |
Check if the Item exists or not:
Example
Keywords give us the facility to check whether a specified key is present in a dictionary or not:
Code
dic = {'key1': 'value1', 'key2': 2}
# Using the get() method to check if the key exists
if dic.get('key2') is not None:
print("Yes, 'key2' exists in this dictionary")
PythonOutput: Yes, ‘key2’ exists in this dictionary |
Print values one by one
We can also print values one by one:
Code
dic = {'key1': 'value1', 'key2': 2}
for x in dic:
print(dic[x])
PythonOutput: value1 2 |
Print Key and Value in Python
We can print keys and values both at the same time by using the item () function in Python.
Code
dic = {'key1': 'value1', 'key2': 2}
for x, y in dic.items():
print(x, y)
PythonOutput: key1 value1 key2 2 |
Length of Dictionary
By using the len () method we can determine how many items exist in the dictionary:
Code
dic = {'key1': 'value1', 'key2': 2}
print(len(dic))
PythonOutput: 2 |
Pop item from Dictionary
Pop Items in the dictionary by using popitem () method will remove the last item:
Code
dic = {'key1': 'value1', 'key2': 2}
dic.popitem()
print(dic)
PythonOutput: {‘key1’: ‘value1’} |
Delete item from Dictionary
We can delete any Items from a dictionary by using the del () method:
Code
dic = {‘key1’: ‘value1’, ‘key2’: 2}
del dic [“key2”]
print (dic)
PythonOutput: {‘key1’: ‘value1’} |
Clear items from the whole Dictionary
We can remove our dictionary Items by using clear () method:
Code
dic = {'key1': 'value1', 'key2': 2}
dic.clear()
print(dic)
PythonOutput: { } |
You can also read:
Author: TCF Editorial
Copyright The Cloudflare.