Dictionary& Its Methods In Python. Topic-23

Dictionary& Its Methods in python

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)
Python

The Output Of This Code Will Be:

Output
{'name': 'Shivam', 'age': 19, 'eligible': True}
Python

In 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'))
Python

The Output Of This Code Will Be:

Output
Shivam
True
Python

II. 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())
Python

The Output Of This Code Will Be:

Output
dict_values(['Shivam', 19, True])
Python

III. 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())
Python

The Output Of This Code Will Be:

Output
dict_keys(['name', 'age', 'eligible'])
Python

IV. 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())   
Python

The Output Of This Code Will Be:

Output
dict_items([('name', 'Shivam'), ('age', 19), ('eligible', True)])
Python

Methods 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)
Python

The Output Of This Code Will Be:

Output
{'name': 'Priti', 'age': 19, 'eligible': True}
{'name': 'Priti', 'age': 20, 'eligible': True, 'DOB': 2001}
Python

2. 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)
Python

The Output Of This Code Will Be:

Output
{}
Python

3. 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)
Python

The Output Of This Code Will Be:

Output
{'name': 'Priti', 'age': 19}
Python

4. 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)
Python

The Output Of This Code Will Be:

Output
{'name': 'Priti', 'age': 19, 'eligible': True}
Python

5. 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)
Python

The Output Of This Code Will Be:

Output
{'name': 'Priti', 'eligible': True, 'DOB': 2003}
Python

6. Delete Complete Dictionary:

info = {'name': 'Priti', 'age': 19, 'eligible': True, 'DOB': 2003}
del info
print(info)
Python

The Output Of This Code Will Be:

Output
NameError: name 'info' is not defined
Python

If 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! 🚀

Finite number of  lines code and infinite possibilities

Programmer, Arpan Bera

Leave a Reply

Your email address will not be published. Required fields are marked *

ABOUT ME
Arpan Bera

Coding is like Love – everyone can start, but only a few finish without errors

Keep Updated to our News and Blog