Table of Contents
- What is a Dictionary?
- Declaring a Dictionary
- Accessing elements of a Dictionary
- Updating a value against a key in Python dict
- Deleting elements of a List
- Looping over a Dictionary in Python
- Frequently used Dictionary methods
What is a Dictionary?
In Python, Dictionary is a generalization of a List. Values in a list are accessed using index that start from 0, whereas values in a Dictionary are accessed using a key
. A key can be any immutable python object. Dictionaries are surrounded by curly braces.
An example comparing a list
and dict
would be helpful:
Now, why should I use a dictionary if I can use a list? Good question! We see that accessing an element in a list requires us to use specific index, which is an integer. However, we can have any other immutable Python object in case of dictionary!
For example:
In dictionary, we can define our own keys. In d = {'i': 'one', 'ii': 'two', 'iii': 'three', 'iv': 'four'}
, keys are ['i', 'ii', 'iii', 'iv']
.
Declaring a Dictionary
Empty dictionary is declared by empty curly braces.
Generalized form of declaring a dictionary is d = {key1: value1, key2: value2 .. .. }
:
Accessing elements of a Dictionary
We use square brackets along with the key whose value we want to access.
Examples:
We get a KeyError
if we try to access using some key which is not present in the dictionary.
A safer way to use the get()
method on a dictionary.
get()
returns None
in case the key is not found in the dict
. If you want to verify this, use print()
function.
Updating a value against a key in Python dict
Dictionaries are mutable, just like lists. We can update a value at using a specific key.
Unlike a List or Tuple, Dictionary does not maintain the order in which you added the keys and values. For instance, if you try to print the dict in the above example, you’ll see a random order.
If we try to update a key which does not exist in the dictionary, python will create a new key and add it to the dict
. For example:
Deleting elements of a List
We can delete a value in a dict
by using a key. For example:
We can also delete an entire dict
using del
In case we want to remove all key: value pairs inside the dict
, we can use dict.clear()
method. This way, the variable will not be destroyed and we can use it in future.
In case we don’t want to delete the key: value pair but would like to remove the value against the key, we can set None
value against that key.
Looping over a Dictionary in Python
dict.items()
returns a list of (key, value) tuples of the dictionary. We can use it iterate/loop over the dictionary.
Frequently used Dictionary methods
Get a list of all the keys of the dictionary.
Similarly, we can get a list of the all the values of the dictionary.
Creating a copy of a dictionary.
Dictionaries are yet another widely used data-structures in Python, just like Lists. It would be interesting to know how it is possible to have various types of keys in dictionary and yet have a fast lookup while accessing the value for that key. Python creates a hash for the key and created internal hash: value map. Let’s not get deeper into this, it’s a post for some other day.
Note: This is a part of what I learned in an online Open Course Ware offered by MIT on edX. Its for my personal reference & also for those who would like to revisit the course.