Python : Dictionaries Method

MethodDescription
clear()clear() मेथड dictionary के सारे elements को remove करता है।
copy()copy() मेथड specific dictionary की copy को return करता है।
fromkeys()Returns a dictionary with the specified keys and value
get()get() मेथड किसी item की value को उसके key के साथ return करता है।
items()items() मेथड प्रत्येक key – value के pair की tuple वाली list को return करता है।
keys()dictionary keys को list में contain करके return करती है।
pop()pop() मेथड dictionary से किसी specific item को remove करता है।
popitem()popitem() मेथड dictionary में last insert item को remove करता है।
setdefault()Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
update()update() मेथड dictionary में specific item को insert करती है।
values()dictionary values को list में contain करके return करती है।

clear()

clear() मेथड dictionary के सारे elements को remove करता है।

Syntax
dictionary.clear()
Emp = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}
Emp.clear()
print(Emp)
OUTPUT
{}

copy()

copy() मेथड specific dictionary की copy को return करता है।

Syntax
dictionary.copy()
fruits = {"mango":5 , "apple" : 6, "banana": 8, "cherry":4}
fruits2 = fruits.copy()
print("copy dict :  ", fruits2)
OUTPUT
copy dict :   {'mango': 5, 'apple': 6, 'banana': 8, 'cherry': 4}

get()

get() मेथड किसी item की value को उसके key के साथ return करता है।

Syntax
dictionary.get(keyname, value)
Keyname : उस आइटम का key name दे जिसकी value आप return चाहते है। 
# value : Optional
student = {"sachin" : 25, "sandeep":26, "deepak": 23,"krishna":24}
stud = student.get("deepak")
print(stud)
OUTPUT
23

items()

items() मेथड प्रत्येक key – value के pair की tuple वाली list को return करता है।

Syntax
dictionary.items()
fruits = {"mango":5 , "apple" : 6, "banana": 8, "cherry":4}
fruits2 = fruits.items()
print(" dict items : ", fruits2)
OUTPUT
dict items :   dict_items([('mango', 5), ('apple', 6), ('banana', 8), ('cherry', 4)])

keys()

keys() मेथड dictionary keys को list में contain करके return करती है।

Syntax
dictionary.keys()
Emp = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}    
K= Emp.keys()
print("dict Keys : ", K)
OUTPUT
dict Keys :  dict_keys(['Name', 'Age', 'salary', 'Company'])

pop()

pop() मेथड dictionary से किसी specific item को remove करता है।

Syntax
dictionary.pop(keyname, value)
#keyname : Required
# value : Optional
student = {"sachin" : 25, "sandeep":26, "deepak": 23,"krishna":24}
student.pop("deepak")
print(student)
OUTPUT
{'sachin': 25, 'sandeep': 26, 'krishna': 24}

popitem()

popitem() मेथड dictionary में last insert item को remove करता है। लेकिन python 3.7 से पहले के versions में popitem() method किसी भी random item को remove करता था।

Syntax
dictionary.popitem()
student = {"sachin" : 25, "sandeep":26, "deepak": 23,"krishna":24}
student.popitem()
print(student)
OUTPUT
{'sachin': 25, 'sandeep': 26, 'deepak': 23}

update()

update() मेथड dictionary में specific item को insert करती है।

Syntax
dictionary.update(iterable)
ar = { "brand": "Ford", "model": "Mustang", "year": 1964}
car.update({"color": "White"})
print(car)
OUTPUT
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'White'}

values()

dictionary values को list में contain करके return करती है।

Syntax
dictionary.values()
Emp = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}    
K= Emp.values()
print("dict values : ", K)
OUTPUT
dict values :  dict_values(['John', 29, 25000, 'GOOGLE'])