
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
python में dictionary एक unordered collection होता है जो { } के अंदर key pair values को store करता है, key pair values को comma से separated किया जाता है ,और हर value को comma separate करते हैं.
# Using curly braces
dict1 = {"name": "Lisa", "age": 24, "study": "Pyrhon"}
print(dict1)
# Output:
# {'name': 'Lisa', 'age': 24, 'study': 'Python'}
# Using dict() method
dict2 = dict(name="Lisa", age=24, study="Python")
print(dict2)
# Output:
# {'name': 'Lisa', 'age': 24, 'study': 'Python'}
dictionary में values को access करने के लिए उसकी key का use किया जाता है
dict1 = {"name": "Retesh", "age": 25, "city": "Delhi"}
print(dict1["name"]) # Outpot: Retesh
dictionary में existing value को change करने के लिए key का use करते हैं
dict1 = {"name": "Retesh", "age": 25, "city": "Delhi"}
print(dict1["name"]) # Outpot: Retesh
dict1["name"] = "Daya"
print(dict1["name"]) # Output: Daya
dictionary में हम new key pair values को आसानी से add कर सकते हैं
dict1 = {"name": "Retesh", "age": 25}
dict1["study"] = "Python"
print(dict1)
# Outpot: {'name': 'Retesh', 'age': 25, 'study': 'Python'}
Dictionary के items को iterate करने के लिए loop का use किया जाता है हम loop का use करके key को iterate कर सकते हैं, value को iterate कर सकते हैं, उन items को भी iterate कर सकते है
dict1 = {"name": "Retesh", "age": 25, "study": "Python"}
# iterate keys
for i in dict1:
print(i)
# iterate values
for i in dict1.values():
print(i)
# iterate items
for i in dict1.items():
print(i)
जब हम dictionary के अंदर भी dictionary बनाते हैं तो इस nested dictionary बोलते हैं.
nest_dict = {
"student1": {"name": "Roli", "age": 20},
"student2": {"name": "Roshan", "age": 25}
}
print(nest_dict)
# Output:
# {'student1': {'name': 'Roli', 'age': 20},
# 'student2': {'name': 'Roshan', 'age': 25}}
python मे dictionary के लिए build-in methods होते है, जिनका use करके dictionary के साथ अलग अलग operation perform किया जाता है।
Syntex:
method को किसी भी variable के साथ use करने के लिए dot ( . ) का use करते है।
Example:
fromkey() : इस method का use हम new dictionary को create करने के लिए करते है जिसमे हम new keys लेते है और उसमे same value को assign करते है।
keys = ("Name", "Age", "Study")
values = "NA"
student = dict.fromkeys(keys,values)
print(student)
# Output: {'Name': 'NA', 'Age': 'NA', 'Study': 'NA'}
pop(): Specified key के pair को remove करता है और value return करता है।
popitem(): Last inserted key-value pair को remove करता है।
del: Specified key के pair को delete करता है।
clear(): सारे items को remove करता है।
dict1 = {"name": "Retesh", "age": 25, "study": "Python"}
# Using pop()
dict1.pop("study")
print(dict1)
# Outpot: {'name': 'Retesh', 'age': 25}
# Using popitems()
dict1.popitem()
print(dict1)
# Output : {'name': 'Retesh}
# Using del
del dict1["name"]
print(dict1) # Output: {}
#using clear()
dict1 = {"name": "Retesh", "age": 25, "study": "Python"}
dict1.clear()
print(dict1) # Output: {}
copy(): Shallow copy बनाता है।
dict(): Shallow copy बनाता है।
dict1 = {"name": "Retesh", "age": 25, "study": "Python"}
# Using copy()
dict2 = dict1.copy()
print(dict2)
# Output:
# {'name': 'Retesh', 'age': 25, 'study': 'Python'}
# Using dict()
dict3 = dict(dict1)
print(dict3)
# Output:
# {'name': 'Retesh', 'age': 25, 'study': 'Python'}
Dictionary के कुछ और useful methods :
get(): Specified key की value return करता है।
items(): Dictionary के items (key-value pairs) return करता है।
keys(): Dictionary के keys return करता है।
setdefault(): Specified key की value return करता है, अगर key exist नहीं करती, तो उस key और default value को add करता है।
update(): Specified key-value pairs से dictionary को update करता है।
values(): Dictionary के values return करता है।
उम्मीद करते है कि आपको Python Dictionary अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...
Table of Contents All Chapters 1. Modules in Python 2. Pre-defined Modules 2.1. math 2.2. random...
Table of Contents All Chapters 1. Iterators 1.1. Iterable 1.2. Iterator 1.3. Iterator Protocol...
Table of Contents All Chapters 1. Polymorphism 2. Key Concepts of Polymorphism 3. Types of Polymorphism 3.1. Polymorphism...
Table of Contents All Chapters 1. Inheritance 2. Concepts of Inheritance 3. Types of Inheritance 3.1. Single Inheritance...
Table of Contents All Chapters 1. Classes and Objects 2. Class in Python 3. Class Structure 3.1. Attributes...
Table of Contents All Chapters 1. Python Function 2. Define Function 3. Function Arguments 4. Function Parameters 5. Local and...
Table of Contents All Chapters 1. Python Dictionary 2. Access Dictionary items 3. Change Dictionary items 4. Add items in...