We have stored values in variables and lists. In lists, the values were accessed using indices. Dictionary stores values using keys. Keys in dictionaries map directly to the values. It is like a hash table. It is enclosed in curly braces ({}) and every key and its value are separated by commas. A colon (:) separates the key and its value. It is just like English dictionary, the keys are the words and the values are their meanings.

Dictionary in Python

There is no concept of order of elements in dictionaries. The result may have a different order of elements compared to your order. You can declare a null dictionary by leaving the space between curly braces blank.

Important Points to Remember – Dictionary

  1. You can specify the value of a key by entering the key variable in square brackets followed by the variable specifying the dictionary and then give the value for the key OR you can directly specify values to various keys in just one statement by entering the key and the value separated by colon end all the keys and their values enclosed in curly braces.
  2. To print out the value of a key in a dictionary, just specify the key along with the dictionary variable in square brackets.
  3. Keys and values are the objects of class ‘Dictionary’. So, you can call the objects anytime anywhere in the program. Objects are called by mentioning the object name after a full stop and a pair of parenthesis after it.
  4. You can get all the keys of a dictionary (and not the values) by mentioning the dict. object name and calling the ‘keys’ method. Same goes for the values. All you have to do is mention the dict. object name and call the ‘values’ method. All the values of that dict. will be printed out.

Slicing of strings just by mentioning their starting index and ending index is not possible because there is no concept of order of elements in dictionaries.


dict1 = {}
dict1['one'] = "code in "
dict1[2] = "python"
dict2 = {'name': 'david','rollno':123, 'sec': 'f'}


{'one': 'code in ', 2: 'python'}
code in
{'sec': 'f', 'name': 'david', 'rollno': 123}
dict_keys(['sec', 'name', 'rollno'])
dict_values(['f', 'david', 123])

Akarsh Singh
Follow me

Akarsh Singh

Founder, Code in Python. Web and Android App Developer. He loves Python and wants to share his knowledge via this website. Follow him on Facebook, Twitter and Github.
Akarsh Singh
Follow me

Latest posts by Akarsh Singh (see all)