Python dictionary Methods

📔 : Python 🔗

Python में dictionary के कई predefined methods कुछ तरह हैं -

  1. get()
  2. keys()
  3. values()
  4. items()
  5. update()
  6. pop()
  7. popitem()
  8. clear()
  9. setdefault()
  10. copy()
  11. fromkeys()

1. Python get()

Python में किसी particular dictionary items को access करने के लिए get() method का use भी कर सकते हैं।

Python get() Example
Copy Fullscreen Close Fullscreen Run
d = {
    'name' : 'Rahul Kumar Rajput',
    'age' : 25,
    'city' : 'Agra',
    'country' : 'India'
}

#access name.
print('Name :', d.get('name'))

#access city.
print('City :', d.get('city'))
Output
C:\Users\Rahulkumar\Desktop\python>python dict_get.py
Name : Rahul Kumar Rajput
City : Agra

Python keys()

किसी dictionary की सभी keys extract करने के लिए keys() method का use किया जाता है। keys() method सभी keys की list return करता है।

Python keys() Example
Copy Fullscreen Close Fullscreen Run
d = {
    'name' : 'Rahul Kumar Rajput',
    'age' : 25,
    'city' : 'Agra',
    'country' : 'India'
}
print(d.keys())
Output
C:\Users\Rahulkumar\Desktop\python>python dict_keys.py
dict_keys(['name', 'age', 'city', 'country'])

Python values()

किसी dictionary की सभी values extract करने के लिए values() method का use किया जाता है। values() method सभी dictionary values की list return करता है।

Python values() Example
Copy Fullscreen Close Fullscreen Run
d = {
    'name' : 'Rahul Kumar Rajput',
    'age' : 25,
    'city' : 'Agra',
    'country' : 'India'
}
print(d.values())
Output
C:\Users\Rahulkumar\Desktop\python>python dict_values.py
dict_values(['Rahul Kumar Rajput', 25, 'Agra', 'India'])

Python items()

items() method, dictionary में present हर key : value pair का एक tuple बनाकर एक list return है। मतलब items() method tuples की list return करता है, उस list में मौजूद हर एक tupleमें दो item होते हैं - पहला key , दूसरा उस key से associate value.

Python items() Example
Copy Fullscreen Close Fullscreen Run
d = {
    'name' : 'Rahul Kumar Rajput',
    'age' : 25,
    'city' : 'Agra',
    'country' : 'India'
}
print(d.items())
Output
C:\Users\Rahulkumar\Desktop\python>python dict_items.py
dict_items([('name', 'Rahul Kumar Rajput'), ('age', 25), ('city', 'Agra'), ('country', 'India')])

Python update()

dictionary का update() method new item add और existing item को update करने के लिए use कर सकते हैं। जो dictionary को as a parameter accept करता है , जिसमे हम updated values को key के साथ रखते हैं। अगर key exist है तो value update हो जायगी और अगर new हुई तो नया item add हो जायगा।

Python update() Example
Copy Fullscreen Close Fullscreen Run
d = {
  'name' : 'Rahul',
}
print('Before update :', d)
#update name.
d.update({'name':'Rahul Kumar Rajput'})

#add new item : country.
d.update({'country':'India'})
print('After update :', d)
Output
C:\Users\Rahulkumar\Desktop\python>python dict_update().py
Before update : {'name': 'Rahul', 'age': 25}
After update : {'name': 'Rahul Kumar Rajput', 'age': 25, 'country': 'India'}

Python pop()

pop() method का use करके आप किसी specified item को delete कर सकते हैं , यह method एक parameter as a value accept करता है जिसे हमें delete करना है।

Python pop() Example
Copy Fullscreen Close Fullscreen Run
d = {
  'name' : 'Rahul',
  'age' : 25,
  'country' : 'India',
  'other' : 'Other Things'
}
print('Before delete :', d)

#delete age.
d.pop('age')

#delete other.
d.pop('other')

print('After delete :', d)
Output
C:\Users\Rahulkumar\Desktop\python>python dict_pop.py
Before delete : {'name': 'Rahul', 'age': 25, 'country': 'India', 'other': 'Other Things'}
After delete : {'name': 'Rahul', 'country': 'India'}

Python popitem()

poppopitem) method का use dictionary से last item को delete करने के लिए use किया जाता है , यह भी एक parameter as a value accept करता है जिसे हमें delete करना है।

Python popitem() Example
Copy Fullscreen Close Fullscreen Run
d = {
  'name' : 'Rahul',
  'age' : 25,
  'country' : 'India',
}
print('Before delete :', d)

#delete country.
d.popitem()
#delete other.
d.popitem()

print('After delete :', d)
Output
C:\Users\Rahulkumar\Desktop\python>python dict_popitem.py
Before delete : {'name': 'Rahul', 'age': 25, 'country': 'India'}
After delete : {'name': 'Rahul'}

Python clear()

clear() method का use dictionary को completely empty करने के लिए किया जाता है।
See Example -

dict_var.clear()

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers