Dictionary:
In Python programming, we use the dictionary in the form of “key-value” pairs. The dictionary is a collection of unordered lists, where every item is associated with a unique key. Dictionaries are defined using curly braces {} and consist of comma-separated key-value pairs. Let’s see an example:
info = {'name': 'Shivam', 'age': 19, 'eligible': True}
print(info)
PythonThe Output Of This Code Will Be:
{'name': 'Shivam', 'age': 19, 'eligible': True}
PythonIn this example, info is a dictionary with three key-value pairs. Now, let’s understand how to access information from dictionaries:
Access The Dictionary Items:
I. Access The Single Value:
- Values can be accessed by corresponding keys
- We can use square brackets or get method.
Let’s see an example for a better understanding:
print(info['name'])
print(info.get('eligible'))
PythonThe Output Of This Code Will Be:
Shivam
True
PythonII. Access Multiple Values:
- We can print all values using the values() method.
Let’s see an example:
info = {'name': 'Shivam', 'age': 19, 'eligible': True}
print(info.values())
PythonThe Output Of This Code Will Be:
dict_values(['Shivam', 19, True])
PythonIII. Access Keys:
- We can print all values using the keys() method.
Let’s see an example for a better understanding:
info = {'name': 'Shivam', 'age': 19, 'eligible': True}
print(info.keys())
PythonThe Output Of This Code Will Be:
dict_keys(['name', 'age', 'eligible'])
PythonIV. Access Key-value Pairs:
We can print all values in the form of key-value pairs using the `items()` method
Let’s see an example for a better understanding:
info = {'name': 'Shivam', 'age': 19, 'eligible': True}
print(info.items())
PythonThe Output Of This Code Will Be:
dict_items([('name', 'Shivam'), ('age', 19), ('eligible', True)])
PythonMethods Of Dictionary
1. update():
Using the `update()` method, we can update the existing values of the dictionary. If the value does not exist, a new key-value pair is created. Let’s understand through an example:
info = {'name': 'Priti', 'age': 19, 'eligible': True}
print(info)
info.update({'age': 20})
info.update({'DOB': 2001})
print(info)
PythonThe Output Of This Code Will Be:
{'name': 'Priti', 'age': 19, 'eligible': True}
{'name': 'Priti', 'age': 20, 'eligible': True, 'DOB': 2001}
Python2. clear():
Using the `clear()` method, we can remove all existing elements from the dictionary. Let’s see an example:
info = {'name': 'Priti', 'age': 19, 'eligible': True}
info.clear()
print(info)
PythonThe Output Of This Code Will Be:
{}
Python3. pop():
The `pop()` method is used to remove a specific key along with its corresponding value from the dictionary. Let’s see an example:
info = {'name': 'Priti', 'age': 19, 'eligible': True}
info.pop('eligible')
print(info)
PythonThe Output Of This Code Will Be:
{'name': 'Priti', 'age': 19}
Python4. popitem():
The `popitem()` method is used to remove the last key-value pair from the dictionary.
info = {'name': 'Priti', 'age': 19, 'eligible': True, 'DOB': 2003}
info.popitem()
print(info)
PythonThe Output Of This Code Will Be:
{'name': 'Priti', 'age': 19, 'eligible': True}
Python5. del():
- Using the `del()` keyword, we can remove the item from the dictionary.
- If a specific key is provided, the corresponding key-value pair is removed.
- If the key is not provided, the dictionary will be completely deleted.
Let’s understand through an example:
info = {'name': 'Priti', 'age': 19, 'eligible': True, 'DOB': 2003}
del info['age']
print(info)
PythonThe Output Of This Code Will Be:
{'name': 'Priti', 'eligible': True, 'DOB': 2003}
Python6. Delete Complete Dictionary:
info = {'name': 'Priti', 'age': 19, 'eligible': True, 'DOB': 2003}
del info
print(info)
PythonThe Output Of This Code Will Be:
NameError: name 'info' is not defined
PythonIf you don’t know the “Set & Set Operations In Python” just click on Read More.
I hope you understand “Dictionary& Its Methods In Python” Need guidance or have questions? Drop a comment below. Share your email for a personalized touch—I’ll send exclusive resources and answer queries. Let’s code together, creating efficient code and memorable learning moments! 🚀