Dictionary Methods (डिक्शनरी के Methods) :-
Dictionary Methods (डिक्शनरी के Methods) Python में उपलब्ध Built-in Methods हैं, जिनका उपयोग Dictionary के Elements को Access, Add, Update, Remove और Manage करने के लिए किया जाता है।
English
Dictionary Methods are built-in methods available in Python that are used to Access, Add, Update, Remove, and Manage elements of a Dictionary.
1. get() Method :-
get() Method का उपयोग Dictionary से किसी Key की Value को Access करने के लिए किया जाता है।
English
The get() method is used to access the Value of a specific Key from a Dictionary.
Syntax -
dictionary_name.get(key)
Example -
student = {
"name": "Rahul",
"age": 20
}
print(student.get("name"))
Output-
Rahul
2. keys() Method :-
keys() Method Dictionary में मौजूद सभी Keys को Return करता है।
English
The keys() method returns all the Keys present in a Dictionary.
Example -
student = {
"name": "Rahul",
"age": 20,
"course": "O Level"
}
print(student.keys())
Output-
dict_keys(['name', 'age', 'course'])
3. values() Method :-
values() Method Dictionary में मौजूद सभी Values को Return करता है।
English
The values() method returns all the Values present in a Dictionary.
Example -
print(student.values())
Output-
dict_values(['Rahul', 20, 'O Level'])
4. items() Method :-
items() Method Dictionary के सभी Key-Value Pairs को Return करता है।
English
The items() method returns all the Key-Value Pairs of a Dictionary.
Example -
print(student.items())
Output-
dict_items([
('name', 'Rahul'),
('age', 20),
('course', 'O Level')
])
5. update() Method :-
update() Method का उपयोग Dictionary में नए Elements Add करने या Existing Elements की Values Update करने के लिए किया जाता है।
English
The update() method is used to add new Elements or update existing Values in a Dictionary.
Example -
student = {
"name": "Rahul",
"age": 20
}
student.update({
"age": 21,
"course": "O Level"
})
print(student)
Output-
{'name': 'Rahul', 'age': 21, 'course': 'O Level'}
6. pop() Method :-
pop() Method का उपयोग Dictionary से किसी Specific Key-Value Pair को Remove करने के लिए किया जाता है।
English
The pop() method is used to remove a specific Key-Value Pair from a Dictionary.
Example -
student = {
"name": "Rahul",
"age": 20,
"course": "O Level"
}
student.pop("age")
print(student)
Output-
{'name': 'Rahul', 'course': 'O Level'}
7. popitem() Method :-
popitem() Method Dictionary से Last Inserted Key-Value Pair को Remove करता है।
English
The popitem() method removes the last inserted Key-Value Pair from a Dictionary.
Example -
student = {
"name": "Rahul",
"age": 20,
"course": "O Level"
}
student.popitem()
print(student)
Output-
{'name': 'Rahul', 'age': 20}
8. clear() Method :-
clear() Method Dictionary के सभी Elements को Remove कर देता है और Dictionary को Empty कर देता है।
English
The clear() method removes all Elements from a Dictionary and makes it Empty.
Example -
student = {
"name": "Rahul",
"age": 20
}
student.clear()
print(student)
Output-
{}
9. copy() Method :-
copy() Method Dictionary की Shallow Copy बनाने के लिए उपयोग किया जाता है।
English
The copy() method is used to create a Shallow Copy of a Dictionary.
Example -
student = {
"name": "Rahul",
"age": 20
}
new_student = student.copy()
print(new_student)
Output-
{'name': 'Rahul', 'age': 20}
10. setdefault() Method :-
setdefault() Method किसी Key की Value को Return करता है। यदि Key मौजूद नहीं है, तो यह उस Key को दी गई Default Value के साथ Dictionary में Add कर देता है।
English
The setdefault() method returns the Value of a Key. If the Key does not exist, it adds the Key with the specified Default Value.
Syntax -
dictionary_name.setdefault(key, default_value)
Example -
student = {
"name": "Rahul",
"age": 20
}
student.setdefault("course", "O Level")
print(student)
Output-
{'name': 'Rahul', 'age': 20, 'course': 'O Level'}
11. fromkeys() Method :-
fromkeys() Method दी गई Keys से एक नई Dictionary बनाने के लिए उपयोग किया जाता है और सभी Keys को एक समान Value दी जा सकती है।
English
The fromkeys() method is used to create a new Dictionary from given Keys, optionally assigning the same Value to all Keys.
Syntax -
dict.fromkeys(keys, value)
Example -
keys = ["name", "age", "course"]
student = dict.fromkeys(keys, "Not Available")
print(student)
Output-
{'name': 'Not Available', 'age': 'Not Available', 'course': 'Not Available'}