Python Tutorial

What is Python Dictionary? How to Create Dictionary in Python?

What is Python Dictionary?

A dictionary in Python is an unordered group or collection of data values that may be used to store data values in the same way that a map can. 


Unlike other data types, which can only carry a single value as an element, dictionaries can hold a key-value pair. The dictionary includes a key-value pair to make it more efficient.

How to Create a Dictionary in Python?

A dictionary is built in Python by enclosing a succession of entries in curly braces and separating them with a comma. 

Dictionary stores a pair of values, one of which is the Key and the other is the Key-value pair element. In a dictionary, values can be of any data type and can be replicated, but keys can’t be copied and must be immutable.

Note: Dictionary keys are case-sensitive. Therefore two keys with the same name but different cases will be interpreted differently.

# Creating a Dictionary

# with Integer Keys

myDict = {1: 'WSC', 2: 'For', 3: 'WSC'}


# Creating a Dictionary

# with Mixed keys

myDict = {'Name': 'WSC', 1: [1, 2, 3]}

print("\nDictionary with Mixed Keys: ")



{1: 'WSC', 2: 'For', 3: 'WSC'}
Dictionary with Mixed Keys: 
{1: [1, 2, 3], 'Name': 'WSC'}

You can also create the dictionary with built-in function dict(). To create empty dictionary, use curly bracket {}.

# Creating an empty Dictionary

myDict = {}

print("Empty Dictionary: ")


# Creating a Dictionary

# with dict() method

myDict = dict({1: 'WSC', 2: 'For', 3:'WSC'})

print("\nDictionary with the use of dict(): ")


# Creating a Dictionary

# with each item as a Pair

myDict = dict([(1, 'WSC'), (2, 'For')])

print("\nDictionary with items as pair: ")



Empty Dictionary: 
Dictionary with the use of dict(): 
{1: 'WSC', 2: 'For', 3: 'WSC'}
Dictionary with items as pair: 
{1: 'WSC', 2: 'For'}

How to Access Data From a Dictionary?

To get to the things in a dictionary, look up the key name. Inside square brackets, the key can be used.

Dict = {1: 'WSC', 'name': 'For', 3: 'WSC'}

# accessing a element using key

print("Accessing a element using key:")


# accessing an element using key

print("Accessing an element using key:")



Accessing an element using key:
Accessing an element using key:

There is also a get() function that may be used to obtain an element from a dictionary.

# Creating a Dictionary

Dict = {1: 'WSC', 'name': 'For', 3: 'WSC'}

# accessing an element using get()

# method

print("Accessing an element using get:")



Accessing an element using get:

How to Update data in Python Dictionary?

The dictionary is updated with items from another dictionary object or an iterable of key/value pairs using the update() function.


marks = {'Physics':67, 'Maths':87}

internal_marks = {'Practical':48}




{'Physics': 67, 'Maths': 87, 'Practical': 48}
Did you find this article helpful?