Python Dictionaries

In python when you work with string, lists and tuples then you’re using integers as indices. You’re not allowed to use any other data type for indices and using it in a program will give you an error. If you want to use any other type than integer your only option is to use dictionaries.

What is a dictionary?

To put it simply, dictionary is a collection of key-value pair that mapped from key to value.

Some important points about Dictionaries –

  • A dictionary is an associative array (hash).
  • Dictionaries are unordered sets (or key-value pairs).
  • Every key of the dictionary is mapped to a value.
  • They’re accessed via their keys and not with their position.
  • Dictionaries are mutable.

How to Create a Dictionary?

Whenever you create a dictionary, you need to start with the empty key set.

Python Dictionary (empty)

langsc={}

This creates our lang dictionary and now we need to add elements inside the dictionary.

langsc['india']='hindi'
langsc['japan']='kanji'
langsc['uk']='english'

Python Dictionary - (Key mapped to Value)

These are the elements that are added to our dictionary of lang. Each country key is mapped to the script value. So for japan country key the mapped value is kanji. If you want to print this dictionary to view the result, type the following code.

print lang

{'japan': 'kanji', 'india': 'hindi', 'uk': 'english'}

This will print out the elements from the dictionary. Note that dictionary is unordered set of collection and that is why any element from the dictionary will be placed in random order. So output for you is different from what you see in this page. As dictionary values are mapped with keys, lets see if we can map our values in dictionary.

print langsc['uk']
english

The key uk will map to english value and you get the result in the output. Let’s try to access key that doesn’t exist.

langsc['us']

Typing and executing the above code wil throw an error as there is no ‘us’ key. You can iterate over a dictionary using key.

syntax:

for key in dictionary
print key

code:
for japan in langsc
print japan

You can also use dictionary.iterkeys() method to perform the same operation.

Deleting Key-Value from Dictionary

In order to delete the key-value pair from the dictionary you have to use ‘del’ statement.

Syntax:

del dictionary['key']

Code: del langsc['japan']

When you execute the above code and print out the dictionary you’ll notice that key ‘japan’ is no longer in the dictionary.

Calculating Number of Key-value pairs in Dictionary

syntax: len(dictionary)

code : len(langsc)

This will print the number of key-value pairs in the dictionary.

Merging Two Dictionaries

You can also merge two dictionary and change the key-value pairs in any one dictionary. Let’s say we have a dictionary lang1 and lang2 and we want to copy all the key-value pair of lang2 to lang1.

lang1={"india":"hindi","japan":"kanji","uk":"english"}
lang2={"us":"english","pakistan":"urdu"}
lang1.update(lang2)
print(lang1)

You can see the lang1 dictionary have new updated key-value pairs from lang2 dictionary.

So this covers our small tutorial on dictionaries. Hope this helps you understand dictionary in python. If you have any question, feel free to post it in comments.