Python Tutorial for Beginners

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'}

print(myDict)

# Creating a Dictionary

# with Mixed keys

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

print("\nDictionary with Mixed Keys: ")

print(myDict)

Output

{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: ")

print(myDict)

# Creating a Dictionary

# with dict() method

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

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

print(myDict)

# Creating a Dictionary

# with each item as a Pair

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

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

print(myDict)

Output: 

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:")

print(Dict['name'])

# accessing an element using key

print("Accessing an element using key:")

print(Dict[1])

Output: 

Accessing an element using key:
For
Accessing an element using key:
WSC

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:")

print(Dict.get(3))

Output: 

Accessing an element using get:
WSC

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.

Example:

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

internal_marks = {'Practical':48}

marks.update(internal_marks)

print(marks)

Output

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