Dictionaries resemble to hashtables in JAVA, in fact, we can use dictionaries as hashtable in python. We will also see about how we can do that in a post later on. Right now, we will focus on basics of dictionary. Following are some of its characteristics:

  • It consists of key-value pair. The whole pair is called an item of the dictionary.
  • Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces.
  • An empty dictionary without any items is written with just two curly braces, like this: {}.
  • Keys are unique within a dictionary while values may not be.
  • The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.

We will now see about how to create a dictionary and access values from it.

Creating Dictionaries in Python and Accessing its Values

Let us first create a dictionary dict and put some items in it as below:

To access dictionary values, you can use the familiar square brackets along with the key to obtain its value. Consider our dictionary dict for this Example:

The output of the above code is:

If we attempt to access a data item with a key, which is not part of the dictionary, we get a KeyError. For example, in the above code, if I write

I get this error:

Updating Dictionaries in Python

You can update a dictionary by adding a new entry or a key-value pair, modifying an existing entry, or deleting an existing entry as shown below in the simple example −

On executing the above code we get:

Deletion in Dictionaries in Python

You can either remove individual dictionary elements or clear the entire contents of a dictionary. You can also delete entire dictionary in a single operation.

To explicitly remove an entire dictionary, just use the del statement. Example of both the ways is shown below:

You may get an error while using del as it doesn’t exist anymore.

Compared to other sequences, dictionaries store everything in the form of key-value pair. So let us go in more depth about them.

As far as values are concerned, there are no restrictions on values. Any type and any length of information or data can be stored as value in the dictionary. Also, values can be any arbitrary Python object, either standard objects or user-defined objects.

Although, there are some restrictions on using keys. Thus, we focus on that.

Properties of Keys

There are two important points to remember about dictionary keys −

  1. More than one entry per key not allowed. Which means no duplicate key is allowed. When duplicate keys encountered during assignment, the last assignment wins.
  2. Keys must be immutable. Which means you can use strings, numbers or tuples as dictionary keys but something like [‘key’] is not allowed.

Just like strings, lists and tuples, dictionaries also have some built-in functions. Let’s go to that now.

Built-in Functions of Dictionaries

cmp(dict1, dict2)
Compares elements of both dict.

Gives the total length of the dictionary. This would be equal to the number of items in the dictionary.

Produces a printable string representation of a dictionary

Returns the type of the passed variable. If passed variable is dictionary, then it would return a dictionary type.

Now we will take an example which demonstrates some important functions of the dictionaries listed above.

Click here to Copy the Code

On executing the above code we get:

Click here to Copy the Code

Dictionaries also have some built-in methods. We will see all of them and then take an example which covers some important methods.

Built-in Methods of Dictionary

Removes all elements of dictionary dict

Returns a shallow copy of dictionary dict

Create a new dictionary with keys from seq and values set to value.

dict.get(key, default=None)
For key key, returns value or default if key not in dictionary

Returns true if key in dictionary dict, false otherwise

Returns a list of dict’s (key, value) tuple pairs

Returns list of dictionary dict’s keys

dict.setdefault(key, default=None)
Similar to get(), but will set dict[key]=default if key is not already in dict

Adds dictionary dict2’s key-values pairs to dict

Returns list of dictionary dict’s values


Click here to Copy the Code

The above code gives following output:-

Click here to Copy the Code

So, guys that was all about the basics of dictionaries in python. In the next post, we will discuss the mutability of various sequences with a special focus on list and tuple.