Important Notice:

Course Content
Dictionary in Python (Chapter-9) M3-R5
About Lesson

pop():-

            pop() method में दिए गए key को remove करके उसकी value return की जाती है। अगर दी हुई key नहीं मिलती तो set की हुई default value return की जाती है

dict1 = {“one”:’Ramesh’, “two”:’suresh’, “three”:’rajesh’}

print(“Before Removing item :”)

print(dict1)                         # {‘one’: ‘Ramesh’, ‘two’: ‘suresh’, ‘three’: ‘rajesh’}

dict1.pop(“one”)

print(“After Removing item :”)

print(dict1)                                          # {‘two’: ‘suresh’, ‘three’: ‘rajesh’}

*              dict1 = {“one”:’Ramesh’, “two”:’suresh’, “three”:’rajesh’}

print(dict1)                                      # {‘one’: ‘Ramesh’, ‘two’: ‘suresh’, ‘three’: ‘rajesh’}

print(dict1.pop(“four”, “mahesh”))                                   # Mahesh

 

*              dict1 = {“one”:’Ramesh’, “two”:’suresh’, “three”:’rajesh’}

print(dict1)                                 # {‘one’: ‘Ramesh’, ‘two’: ‘suresh’, ‘three’: ‘rajesh’}

print(dict1.pop(“four”))                                 # KeyError: ‘four’

 

Note:- अगर key नहीं मिलती और default value भी नहीं set नहीं की जाती है तो ‘keyError’ exception आता है

 

Rename key name using pop() Method

dict1 = {“one”:’Ramesh’, “two”:’suresh’, “three”:’rajesh’}

print(“Before Changing Key :”)

print(dict1)                        # {‘one’: ‘Ramesh’, ‘two’: ‘suresh’, ‘three’: ‘rajesh’}

dict1[“four”] = dict1.pop(“three”)

print(“After Changing Key :”)

print(dict1)                            # {‘one’: ‘Ramesh’, ‘two’: ‘suresh’, ‘four’: ‘rajesh’}

 

 

pop():-

             In pop() method, the given key is removed and its value is returned. If the given key is not found then the set default value is returned.

dict1 = {“one”:’Ramesh’, “two”:’suresh’, “three”:’rajesh’}

print(“Before Removing item :”)

print(dict1)           # {‘one’: ‘Ramesh’, ‘two’: ‘suresh’, ‘three’: ‘rajesh’}

dict1.pop(“one”)

print(“After Removing item :”)

print(dict1)                        # {‘two’: ‘suresh’, ‘three’: ‘rajesh’}

 

*              dict1 = {“one”:’Ramesh’, “two”:’suresh’, “three”:’rajesh’}

print(dict1)                                 # {‘one’: ‘Ramesh’, ‘two’: ‘suresh’, ‘three’: ‘rajesh’}

print(dict1.pop(“four”, “mahesh”))                                   # Mahesh

 

*              dict1 = {“one”:’Ramesh’, “two”:’suresh’, “three”:’rajesh’}

print(dict1)                                  # {‘one’: ‘Ramesh’, ‘two’: ‘suresh’, ‘three’: ‘rajesh’}

print(dict1.pop(“four”))                  # KeyError: ‘four’

Note:- If the key is not found and the default value is not set then ‘keyError’ exception occurs.

error: Content is protected !!