Python Access dictionary Items

📔 : Python 🔗

अगर हम dictionary के किसी particular item को access करना चाहते हैं तो dictionary में defined key से access कर सकते हैं।

Python Access dictionary Items Example
Copy Fullscreen Close Fullscreen Run
d = {
    'name' : 'Rahul Kumar Rajput',
    'age' : 25,
    'city' : 'Agra',
    'country' : 'India'
}

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

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

Python get()

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

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 Getting Dictionary Keys And Values

values() method का use करके आप dictionary में defined सभी keys की values को extract कर सकते हैं। values() method , dictionary values की list return करता है।


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

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

#access all values.
print(d.values())

#access all keys.
print(d.keys())
Output
C:\Users\Rahulkumar\Desktop\python>python dict_keys_values.py
dict_values(['Rahul Kumar Rajput', 25, 'Agra', 'India'])
dict_keys(['name', 'age', 'city', 'country'])

Python Iterating dictionary with for Loop

अगर आप dictionary के हर item को ही visit करना चाहते हैं , तो for loop का use करके easily कर सकते हैं। for loop में आपको key मिलती है जिसकी help से उस key से associated value को access कर सकते हैं।

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

#we will get dictionary key in every iteration.
for key in d :
    print(key, ' : ', d[key])
Output
C:\Users\Rahulkumar\Desktop\python>python dict_iterate.py
name  :  Rahul Kumar Rajput
age  :  25
city  :  Agra
country  :  India

बैसे तो आप , दो variables pass करके , key : value दोनों चीज़ें एक साथ access कर सकते हो। but उसके लिए हमें items() method के ऊपर iterate करना पड़ेगा।

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

print(d.items())
#we can key : value pair using items() method.
for key,value in d.items() :
    print(key, ' : ', value)
Output
C:\Users\Rahulkumar\Desktop\python>python dict_iterate.py
dict_items([('name', 'Rahul Kumar Rajput'), ('age', 25), ('city', 'Agra'), ('country', 'India')])
name  :  Rahul Kumar Rajput
age  :  25
city  :  Agra
country  :  India

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