Getting Started With Python Dictionaries: Python Tips

Dictionary in Python is a data structure that lets users store and retrieve things in python. As the name “dictionary” suggests, it has “key” and “value”. Needless to say each key has to be unique, but you can have duplicates in values.

How to Create a Dictionary in Python?

An easy way to create dictionary in Python is to create two lists, one for keys and other for values. Let us say you two lists named myKeys and myValues containing keys and values for the dictionary. The beautiful python command

>zipped = zip(myKeys,myValues)
>myDict = dict(zipped)

will create a diction from the two lists in a single step. The “zip” command creates a tuple containing a key-value pair and the “dict” command converts the tuple to a dictionary.

How to Get a Key’s Value from Dictionary?

It is straightforward to get the values of a key from Python dictionary. The way to do is

myDict[myKey]

If the dictionary has the key, it will give the value. If teh dictionary does not have the key. then it will throw the “Key Error” that will look like

>myDict[noKey]
Traceback (most recent call last):
File "", line 1, in
KeyError: noKey

How to List all the Keys and Values in a Dictionary in Python?

Python dictionary object has a way see all the keys as well as all values. To see all the keys in a dictionary, use

myDict.keys()

where “myDict” is the dictionary object that you created.

Listing all the values is very similar and intuitive. To see all the values in your dictionary, use

myDict.values()

Both “keys” and values return lists

How to Check If a Key is in the Dictionary?

Often you may want to check if the “key”is present in the dictionary before trying to get the “values” corresponding to the key. One way to see if a key is present in the dictionary is the the dictionary’s built-in method “has_key()”, like

myDict.has_key(myKey)

The above command will return a 1 if the dictionary has that key and 0 otherwise. Another way to check if a key is present in a dictionary is use like

myKey in myDict

This command behaves very similar to “has_key()” and return 1 or 0 depending whether the key is present or absent.

How to Find the Number Keys-Value Pairs in Python Dictionary?

Finding the size of dictionary or the number of key-value pairs in a dictionary is pretty easy in Python. You can use the built-in function “len()” and get the total number of items in a dictionary.

len(myDict)

How to Clear or Reset or Empty a Dictionary?

If you want to clear all the elements in a dictionary and get a empty dictionary, use the “clear()” command as

myDict.clear()

How to Merge Two Dictionaries in to a Single Dictionary in Python?

If you have two Python dictionaries and want to merge into a single dictionary, use command “update” to update one dictionary with another. For example

myDict.update{myDict2)

will add myDict2’s key-value pairs to the dictionary myDict. Remember that if there are duplicate keys, the updating operation will update first dictionary’s key-values with second dictionary’s.