Important Notice:

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

popitem():-

           popitem() method में दिए गए dictionary के last item को remove करके (key, value) return करता है

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

print(“Before Removing last item : “)

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

print(“Removed item :”,dict1.popitem())    # Removed item : (‘three’, ‘rajesh’)

print(“After Removing last item : “)

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

setdefault():-

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

Syntax:-     dict.setdefault(key, default)

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

print(dict1.setdefault(“one”))                      # Ramesh

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

print(dict1.setdefault(“four”))                        # None

Note:- अगर key dictionary पर नहीं होती है और default parameter का इस्तेमाल नहीं किया जाता है तो ‘None’ return होता है

keys():-

            keys() method से dictionary के सिर्फ keys को return किया जा सकता है

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

print(dict1.keys())                                # dict_keys([‘one’, ‘two’, ‘three’])

values():-

       values() method से dictionary के सिर्फ keys की values को return किया जा सकता है

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

print(dict1.values())                          # dict_values([‘Ramesh’, ‘suresh’, ‘rajesh’])

 

 

 

popitem():-

popitem() method removes the last item of the given dictionary and returns (key, value)

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

print(“Before Removing last item : “)

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

print(“Removed item :”,dict1.popitem())                  # Removed item : (‘three’, ‘rajesh’)

print(“After Removing last item : “)

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

setdefault():-

            If the key given in setdefault() method exists then its value is returned. If there is no key then the set default value is returned.

Syntax:-     dict.setdefault(key, default)

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

print(dict1.setdefault(“one”))                                 # Ramesh

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

print(dict1.setdefault(“four”))                         # None

Note:- If the key is not in the dictionary and no default parameter is used, ‘None’ is returned.

 

keys():-

            Only keys of dictionary can be returned from keys() method.

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

print(dict1.keys())                            # dict_keys([‘one’, ‘two’, ‘three’])

values():-

       With the values() method, only the values of the keys of the dictionary can be returned.

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

print(dict1.values())                        # dict_values([‘Ramesh’, ‘suresh’, ‘rajesh’])

error: Content is protected !!