Important Notice:

Iterating Through a Dictionary

Iterating Through a Dictionary

7 views 3 min read
Iterating Through a Dictionary (डिक्शनरी को Iterate करना) :-
Iterating Through a Dictionary (डिक्शनरी को Iterate करना) का अर्थ Dictionary में मौजूद Elements को एक-एक करके Access या Process करना है।

Python में Dictionary को Iterate करने के लिए मुख्य रूप से for Loop का उपयोग किया जाता है। Dictionary को Iterate करते समय हम Keys, Values या Key-Value Pairs को एक-एक करके प्राप्त कर सकते हैं।

English

Iterating Through a Dictionary means accessing or processing the Elements of a Dictionary one by one.

In Python, the for Loop is mainly used to iterate through a Dictionary. We can iterate through its Keys, Values, or Key-Value Pairs.

1. Iterating Through Keys :-

Dictionary को सीधे for Loop के साथ Iterate करने पर उसकी Keys एक-एक करके प्राप्त होती हैं।

English

When a Dictionary is directly used with a for loop, its Keys are obtained one by one.

Example -

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

for key in student:
    print(key)

Output-

name
age
course
 
2. Iterating Through Values Using values() :-

Dictionary की सभी Values को एक-एक करके Access करने के लिए values() Method का उपयोग किया जाता है।

English

The values() method is used to access all the Values of a Dictionary one by one.

Example -

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

for value in student.values():
    print(value)

Output-

Rahul
20
O Level
 
3. Iterating Through Keys Using keys() :-

Dictionary की सभी Keys को एक-एक करके Access करने के लिए keys() Method का उपयोग किया जा सकता है।

English

The keys() method can be used to access all the Keys of a Dictionary one by one.

Example -

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

for key in student.keys():
    print(key)

Output-

name
age
course
 
4. Iterating Through Key-Value Pairs Using items() :-

Dictionary के Key और Value दोनों को एक साथ एक-एक करके Access करने के लिए items() Method का उपयोग किया जाता है।

English

The items() method is used to access both Keys and Values together, one by one.

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
 
5. Iterating Using for Loop with Condition :-

Dictionary को Iterate करते समय if Condition का उपयोग करके किसी विशेष Condition के आधार पर Data Process किया जा सकता है।

English

An if condition can be used while iterating through a Dictionary to process data based on a specific condition.

Example -

marks = {
    "Rahul": 80,
    "Amit": 65,
    "Ravi": 90
}

for name, mark in marks.items():
    if mark >= 70:
        print(name, mark)

Output-

Rahul 80
Ravi 90
 
6. Iterating Through a Nested Dictionary :-

यदि Dictionary के अंदर दूसरी Dictionary मौजूद हो, तो उसे Nested Dictionary कहा जाता है। Nested Dictionary को भी for Loop की सहायता से Iterate किया जा सकता है।

English

If a Dictionary contains another Dictionary, it is called a Nested Dictionary. A Nested Dictionary can also be iterated using a for loop.

Example -

students = {
    "student1": {
        "name": "Rahul",
        "age": 20
    },
    "student2": {
        "name": "Amit",
        "age": 21
    }
}

for student_id, details in students.items():
    print(student_id)
    print(details["name"])
    print(details["age"])

Output-

student1
Rahul
20
student2
Amit
21
 
7. Iterating and Calculating Values :-

Dictionary को Iterate करके Values पर Calculation भी की जा सकती है।

Example -

marks = {
    "Rahul": 80,
    "Amit": 70,
    "Ravi": 90
}

total = 0

for mark in marks.values():
    total = total + mark

print(total)

Output-

240

यहाँ for Loop द्वारा सभी Marks को एक-एक करके Access करके उनका Total निकाला गया है।

English

Here, the for loop accesses each mark one by one and calculates their total.

Related Notes