How To Access Python Dictionary
Chapter:
Python
Last Updated:
14-06-2021 12:34:46 UTC
Program:
/* ............... START ............... */
thisdict = {
"ID": "1",
"Name": "John",
"Age": 32
}
x = thisdict["Name"]
print(x)
x = thisdict["ID"]
print(x)
/* Output of above program
John
1
*/
// Another method of accessing item.
x = thisdict.get("Name")
print(x)
/* Below prgram will list keys as list * /
thisdict = {
"ID": "1",
"Name": "John",
"Age": 32
}
x = thisdict.keys()
print(x)
/* Output
dict_keys(['ID', 'Name', 'Age'])
*/
/* ............... END ............... */
Notes:
-
You can access the items of a dictionary in python by referring to its key name.
- In square brackets we can pass the key , then it will return value in that place.
- By using the get function is the another method of retrieving the item from dictionary in python. ( eg : x = thisdict.get("Name"))
- keys() function in dictionary will give the all the keys as list. Please refer the program section for more details.
Tags
Python dictionary keys, Python dictionary values , How to access dictionary in python