- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Accessing Values of Dictionary in Python
To access dictionary elements, you can use the familiar square brackets along with the key to obtain its value.
Example
Following is a simple example −
#!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} print "dict['Name']: ", dict['Name'] print "dict['Age']: ", dict['Age']
Output
When the above code is executed, it produces the following result −
dict['Name']: Zara dict['Age']: 7
If we attempt to access a data item with a key, which is not part of the dictionary, we get an error as follows −
Example
#!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} print "dict['Alice']: ", dict['Alice']
Output
When the above code is executed, it produces the following result −
dict['Alice']: Traceback (most recent call last): File "test.py", line 4, in <module> print "dict['Alice']: ", dict['Alice']; KeyError: 'Alice'
- Related Articles
- Accessing Key-value in a Python Dictionary
- Accessing Values of Strings in Python
- Accessing Values of Lists in Python
- Accessing Values of Tuples in Python
- Combining values from dictionary of list in Python
- Python – Inverse Dictionary Values List
- Append Dictionary Keys and Values (In order ) in dictionary using Python
- Extract Unique dictionary values in Python Program
- How to sum values of a Python dictionary?
- How to replace values of a Python dictionary?
- Keys associated with Values in Dictionary in Python
- How to replace values in a Python dictionary?
- Sort Dictionary key and values List in Python
- Find keys with duplicate values in dictionary in Python
- How to update a Python dictionary values?

Advertisements