Accessing Dictionary Elements (डिक्शनरी के Elements को Access करना) :-
Accessing Dictionary Elements (डिक्शनरी के Elements को Access करना) का अर्थ Dictionary में Store किए गए किसी Key-Value Pair की Value को उसकी Key की सहायता से प्राप्त (Access) करना है।
Dictionary में elements को Index Number से नहीं, बल्कि Key की सहायता से Access किया जाता है।
English
Accessing Dictionary Elements means retrieving a stored Value from a Dictionary using its corresponding Key.
Dictionary elements are accessed using their Keys, not by using index numbers.
1. Using Square Brackets [] :-
Dictionary की Value को Access करने का सबसे सामान्य तरीका Square Brackets [] का उपयोग करना है।
English
The most common way to access a Value from a Dictionary is by using Square Brackets [].
Syntax -
dictionary_name[key]
Example -
student = {
"name": "Rahul",
"age": 20,
"course": "O Level"
}
print(student["name"])
Output-
Rahul
यहाँ:
"name" → Key
"Rahul" → Value
"name" Key की सहायता से "Rahul" Value को Access किया गया है।
English
Here, the "Rahul" Value is accessed using the "name" Key.
2. Accessing Multiple Dictionary Elements :-
Dictionary की अलग-अलग Values को उनकी Keys की सहायता से Access किया जा सकता है।
Example -
student = {
"name": "Rahul",
"age": 20,
"course": "O Level"
}
print(student["name"])
print(student["age"])
print(student["course"])
Output-
Rahul
20
O Level
3. Using get() Method :-
Dictionary की Value को Access करने के लिए get() Method का भी उपयोग किया जा सकता है।
English
The get() Method can also be used to access a Value from a Dictionary.
Syntax -
dictionary_name.get(key)
Example -
student = {
"name": "Rahul",
"age": 20,
"course": "O Level"
}
print(student.get("name"))
Output-
Rahul
4. Difference Between [] and get() :-
दोनों methods का उपयोग Value Access करने के लिए किया जाता है, लेकिन दोनों में एक महत्वपूर्ण अंतर है।
English
Both methods are used to access a Value, but there is an important difference between them.
Using []
यदि दी गई Key Dictionary में मौजूद नहीं है, तो KeyError प्राप्त हो सकता है।
Example -
student = {
"name": "Rahul",
"age": 20
}
print(student["city"])
यहाँ "city" Key मौजूद नहीं है, इसलिए KeyError आएगा।
Using get()
यदि Key मौजूद नहीं है, तो get() सामान्यतः None return करता है।
Example -
student = {
"name": "Rahul",
"age": 20
}
print(student.get("city"))
Output-
None
इसलिए जब Key के मौजूद न होने की संभावना हो, तो get() उपयोगी होता है।
5. Using get() with Default Value :-
get() Method में Key के साथ एक Default Value भी दी जा सकती है।
Syntax -
dictionary_name.get(key, default_value)
Example -
student = {
"name": "Rahul",
"age": 20
}
print(student.get("city", "Not Available"))
Output-
Not Available
यहाँ "city" Key मौजूद नहीं है, इसलिए "Not Available" return हुआ।
English
If the specified Key does not exist, the given Default Value is returned.
6. Accessing Keys Using keys() :-
Dictionary की सभी Keys प्राप्त करने के लिए keys() Method का उपयोग किया जाता है।
Example -
student = {
"name": "Rahul",
"age": 20,
"course": "O Level"
}
print(student.keys())
Output-
dict_keys(['name', 'age', 'course'])
English
The keys() method returns all the Keys present in the Dictionary.
7. Accessing Values Using values() :-
Dictionary की सभी Values प्राप्त करने के लिए values() Method का उपयोग किया जाता है।
Example -
student = {
"name": "Rahul",
"age": 20,
"course": "O Level"
}
print(student.values())
Output-
dict_values(['Rahul', 20, 'O Level'])
English
The values() method returns all the Values present in the Dictionary.
8. Accessing Key-Value Pairs Using items() :-
Dictionary के सभी Key-Value Pairs प्राप्त करने के लिए items() Method का उपयोग किया जाता है।
Example -
student = {
"name": "Rahul",
"age": 20,
"course": "O Level"
}
print(student.items())
Output-
dict_items([
('name', 'Rahul'),
('age', 20),
('course', 'O Level')
])
English
The items() method returns all the Key-Value Pairs of the Dictionary.
9. Accessing Dictionary Elements Using for Loop :-
Dictionary के elements को एक-एक करके Access करने के लिए for Loop का उपयोग किया जा सकता है।
Example -
student = {
"name": "Rahul",
"age": 20,
"course": "O Level"
}
for key in student:
print(key)
Output-
name
age
course
10. Accessing Keys and Values Together :-
items() Method और for Loop की सहायता से Key और Value दोनों को एक साथ Access किया जा सकता है।
Example -
student = {
"name": "Rahul",
"age": 20,
"course": "O Level"
}
for key, value in student.items():
print(key, ":", value)
Output-
name : Rahul
age : 20
course : O Level
English
The items() method can be used with a for loop to access both Keys and Values together.
11. Accessing Nested Dictionary Elements :-
यदि Dictionary के अंदर दूसरी Dictionary हो, तो उसे Nested Dictionary कहा जाता है।
Example -
students = {
"student1": {
"name": "Rahul",
"age": 20
},
"student2": {
"name": "Amit",
"age": 21
}
}
print(students["student1"]["name"])
Output-
Rahul
यहाँ पहले "student1" और फिर "name" Key का उपयोग करके Value Access की गई है।
English
Here, the Value is accessed first using the "student1" Key and then using the "name" Key.