Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Dictionaries in python
  5. Accessing elements in a Dictionary

Accessing elements in a Dictionary

A dictionary is set of unordered key-value pairs. In a dictionary, the keys must be unique, and they are stored in an unordered manner.

To build a dictionary, separate the key-value pairs by a colon(‘:’). The keys would need to be of an immutable type, i.e. datatypes for which the keys cannot be changed at runtime such as int, string, tuple, etc.
The values can be of any type. Individual pairs will be separated by a comma(‘,’) and the whole thing will be enclosed in curly braces({….}).

To access the elements of the dictionary we have to use its keys.

To get the values of a dictionary from the keys we can directly reference the keys. To do this we have to enclose the key in square brackets[…] after writing the name of the dictionary.




dict={'1' :'python','2' :'Java','3' :'JavaScript','4' :'C++'}
Output- JavaScript

Was this article helpful to you? Yes No

How can we help?