Important Notice:

Removing Dictionary Elements

Removing Dictionary Elements

6 views 2 min read
Removing Dictionary Elements (डिक्शनरी के Elements को हटाना) :-
Removing Dictionary Elements (डिक्शनरी के Elements को हटाना) का अर्थ Dictionary में मौजूद Key-Value Pair को Dictionary से Delete या Remove करना है।

Python में Dictionary Mutable (परिवर्तनीय) होती है, इसलिए Dictionary बनाने के बाद उसके Elements को Remove किया जा सकता है।

English

Removing Dictionary Elements means deleting or removing a Key-Value Pair from a Dictionary.

Since a Dictionary is Mutable, its Elements can be removed after the Dictionary has been created.

1. Using pop() Method :-

Dictionary से किसी Specific Key-Value Pair को Remove करने के लिए pop() Method का उपयोग किया जाता है।

English

The pop() Method is used to remove a specific Key-Value Pair from a Dictionary.

Syntax -

dictionary_name.pop(key)

Example -

student = {
    "name": "Rahul",
    "age": 20,
    "course": "O Level"
}

student.pop("age")

print(student)

Output-

{'name': 'Rahul', 'course': 'O Level'}

यहाँ: "age": 20 Key-Value Pair Remove हो गया।

2. Using popitem() Method :-

Dictionary से Last Inserted Key-Value Pair को Remove करने के लिए popitem() Method का उपयोग किया जाता है।

English

The popitem() Method is used to remove the last inserted Key-Value Pair from a Dictionary.

Syntax -

dictionary_name.popitem()

Example -

student = {
    "name": "Rahul",
    "age": 20,
    "course": "O Level"
}

student.popitem()

print(student)

Output-

{'name': 'Rahul', 'age': 20}

यहाँ "course": "O Level" Last Inserted Pair था, इसलिए यह Remove हो गया।

3. Using del Keyword :-

Dictionary से किसी Specific Element को Remove करने के लिए del Keyword का उपयोग किया जा सकता है।

English

The del Keyword can be used to remove a specific Element from a Dictionary.

Syntax -

del dictionary_name[key]

Example -

student = {
    "name": "Rahul",
    "age": 20,
    "course": "O Level"
}

del student["age"]

print(student)

Output-

{'name': 'Rahul', 'course': 'O Level'}
 
4. Using clear() Method :-

Dictionary के सभी Elements को Remove करने के लिए clear() Method का उपयोग किया जाता है।

English

The clear() Method is used to remove all Elements from a Dictionary.

Syntax -

dictionary_name.clear()

Example -

student = {
    "name": "Rahul",
    "age": 20,
    "course": "O Level"
}

student.clear()

print(student)

Output-

{}

यहाँ Dictionary delete नहीं हुई है, बल्कि उसके सभी Elements Remove होकर Dictionary Empty हो गई है।

5. Using del to Delete the Entire Dictionary :-

पूरी Dictionary को Delete करने के लिए भी del Keyword का उपयोग किया जा सकता है।

English

The del Keyword can also be used to delete the entire Dictionary.

Example -

student = {
    "name": "Rahul",
    "age": 20
}

del student

अब student Dictionary का variable भी delete हो जाता है।

Difference Between Removing Methods :-
 
Method  कार्य
pop(key)    Specific Key-Value Pair Remove करता है
popitem()   Last Inserted Key-Value Pair Remove करता है
del dict[key]   Specific Key-Value Pair Remove करता है
clear() सभी Elements Remove करता है, Dictionary Empty रहती है
del dict    पूरी Dictionary को Delete करता है

Related Notes