Python Dictionary

Summary: in this tutorial, you will learn about Python dictionary and how to manipulate the dictionary’s elements.

Introducing to Python dictionary

Python dictionary is a built-in type that defines one-to-one relationships between keys and values. Python dictionary is also known as hash table or associative array.

The English dictionary book is a good example of Python dictionary where you can find the meaning of an English word from a specific word. Because a value in the dictionary can be accessed via a unique key, Python does not allows duplicate key in the dictionary.

Defining Python dictionary

Like a list, you can define an empty dictionary or initialize dictionary’s items as follows:

First, we created an empty dictionary that uses curly braces {} instead of square brackets [] like a Python list.

Second, we created stock quotes dictionary that contains three elements. Each element is a key-value pair. For example, AIG is key of the American International Group. Inc.

The dictionary allows you to find a value by a key, but a key by value. For example, you can find company name by stock symbol in the stock quotes dictionary by using the following syntax:

Manipulating dictionary

You can add any new key-value pair to a dictionary with a rule that the added key is unique inside the dictionary. If you assign a new value to an existing key, the old value will wipe out.

You can remove either single element in a dictionary or entire dictionary by using the del statement. The following example demonstrates how to manipulate dictionary:

These are the built-in dictionary object’s methods:

dict.copy( )Returns a shallow copy of the dictionary
dict.has_key(k)Returns true if key is a key in dict; otherwise, returns False, just like key in dict
dict.items( )Returns a new list with all items (key/value pairs) in dict
dict.keys( )Returns a new list with all keys in dict
dict.values( )Returns a new list with all values in dict
dict.iteritems( )Returns an iterator on all items (key/value pairs) in dict
dict.iterkeys( )Returns an iterator on all keys in dict
dict.itervalues( )Returns an iterator on all values in dict
dict.get(key[, x])Returns dict[key] if key is a key in dict; otherwise, returns x(or None, if x is not given)
dict.clear( )Removes all items from dict, leaving dict empty
dict.updictate(dict1)For each key in dict1, sets dict[key] equal to dict1[key]
dict.setdictefault(key[,x])Returns dict[key] if key is found in dict; otherwise, setsdict[key] equal to x and returns x
dict.pop(key[, x])Removes and returns dict[key] if key is found in the dictionary dict; otherwise, returns x or raises an exception if x is not given.
dict.popitem( )Removes and return returns an arbitrary item (key/value pair)

Python dictionary key

Python dictionary allows any immutable object or hash table to be used as a key. Notice that an immutable object is an object that cannot be modified once it is created.

The following table contains data types that can be used as a dictionary key.

Data TypeImmutable?Hashable?Dictionary key?

In this tutorial, we have introduced you to Python dictionary and shown you how to manipulate dictionary effectively.

  • Was this tutorial helpful ?
  • YesNo