Python Dictionary example

Dictionary is a data structure which stores key-value pairs and is very similar to hashmap in java.

Dictionaries won’t retain order, so this is a significant difference with the sequence such as list or tuple.So Dictionaries stores key-value pairs and do not retain order.

A Python dictionary consists of a key and corresponding value. The value can be almost any Python object.

Constructing a Dictionary

Let’s see how we can construct dictionaries to get a better understanding of how they work!

# Create a empty dictionary dict = {} # Create a new key dict['key1'] = 'value1' dict['key2'] = 'value2' print(dict) # You can create dictionary with {} and : is used to denote a key and a value my_dict = {'key1':'value1','key2':'value2'} print(my_dict) # get values by their key print("value corresponding to key2:",my_dict['key2'])


{‘key1’: ‘value1’, ‘key2’: ‘value2’}
{‘key1’: ‘value1’, ‘key2’: ‘value2’}
value corresponding to key2: value2

Please note that you can put any datatype in the dictionary. For example:

Accessing objects from a dictionary

You can access object from dictionary by their keys.Let’s go through some examples to understand it better.

#Create a dictionary my_dict = {'key1':1,'key2':[23,54,65],'key3':'value3'} #Lets get items from the dictionary #Getting key2 from above dictionary print("Value for key2 in my_dict:",my_dict['key2']) # Can call an index on that value print("Let's get first index from value in key2:",my_dict['key2'][0])


Value for key2 in my_dict: [23, 54, 65]
Let’s get first index from value in key2: 23

We can change the values as well. For instance:

my_dict = {'key1':5,'key2':[23,54,65],'key3':'value3'} print("Value for key1:",my_dict['key1']) # Subtract 123 from the value my_dict['key1'] = my_dict['key1'] - 1 print("Value for key1 changed to :",my_dict['key1'])


Value for key1: 5
Value for key1 changed to : 4

Let’s start with an empty dictionary and add some items to it.

# Create a new dictionary dict = {} # Create a new key through assignment dict['name'] = 'John' # Can do this with any object dict['Id'] = 3 print(dict)


{‘name’: ‘John’, ‘Id’: 3}

Nesting with Dictionaries

We can nested Dictionaries too and it is quite interesting to see how it works

# Dictionary nested inside a dictionary nested in side a dictionary dict = {'key1':{'nestkey':{'subnestkey':'value'}}} #Let's see how we can grab that value: # Keep using the keys print(dict['key1']['nestkey']['subnestkey'])



Dictionary Methods

Let’s go through some methods which you might use quite often.

# Create a dictionary d = {'key1':1,'key2':2,'key3':3} # Method to return a list of all keys print("keys:",d.keys()) # Method to grab all values print("values:",d.values()) # Method to return tuples of all items print("key-value pairs:",d.items())


keys: dict_keys([‘key1’, ‘key2’, ‘key3’])
values: dict_values([1, 2, 3])
key-value pairs: dict_items([(‘key1’, 1), (‘key2’, 2), (‘key3’, 3)])

That’s all about Dictionaries in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *