Dictionaries in Python - Lokesh
Dictionaries in Python - Lokesh
Dictionaries in Python
Last Updated : 07 Oct, 2024
A Python dictionary is a data structure that stores the value in key: value pairs.
Example: Here, The data is stored in key: value pairs in dictionaries, which makes it easier to find
values.
Python
Output
Python dictionaries are essential for efficient data mapping and manipulation in programming. To
deepen your understanding of dictionaries and explore advanced techniques in data handling, consider
enrolling in our Complete Machine Learning & Data Science Program. This course covers
everything from basic dictionary operations to advanced data processing methods, empowering you to
become proficient in Python programming and data analysis.
Table of Content
How to Create a Dictionary
Different Ways to Create a Python Dictionary
Nested Dictionaries
Adding Elements to a Dictionary
Accessing Elements of a Dictionary
Accessing an Element of a Nested Dictionary
Dictionaries in Python is a data structure, used to store values in key: value format. This makes it
different from lists, tuples, and arrays as in a dictionary each key has an associated value.
Note: As of Python version 3.7, dictionaries are ordered and can not contain duplicate keys.
In Python, a dictionary can be created by placing a sequence of elements within curly {} braces,
separated by a ‘comma’. The dictionary holds pairs of values, one being the Key and the other
corresponding pair element being its Key:value. Values in a dictionary can be of any data type and
can be duplicated, whereas keys can’t be repeated and must be immutable.
Note – Dictionary keys are case sensitive, the same name but different cases of Key will be treated
distinctly.
The code demonstrates creating dictionaries with different types of keys. The first dictionary uses
integer keys, and the second dictionary uses a mix of string and integer keys with corresponding
values. This showcases the flexibility of Python dictionaries in handling various data types as keys.
Python
Output
Dictionary Example
A dictionary can also be created by the built-in function dict(). An empty dictionary can be created by
just placing curly braces{}.
https://www.geeksforgeeks.org/python-dictionary/ 2/10
11/12/24, 4:54 PM Dictionaries in Python - GeeksforGeeks
The code demonstrates different ways to create dictionaries in Python. It first creates an empty
dictionary, and then shows how to create dictionaries using the dict() constructor with key-value pairs
specified within curly braces and as a list of tuples.
Python
Dict = {}
print("Empty Dictionary: ")
print(Dict)
Output
Empty Dictionary:
{}
Nested Dictionaries
https://www.geeksforgeeks.org/python-dictionary/ 3/10
11/12/24, 4:54 PM Dictionaries in Python - GeeksforGeeks
Example: The code defines a nested dictionary named ‘Dict’ with multiple levels of key-value pairs.
It includes a top-level dictionary with keys 1, 2, and 3. The value associated with key 3 is another
dictionary with keys ‘A,’ ‘B,’ and ‘C.’ This showcases how Python dictionaries can be nested to
create hierarchical data structures.
Python
print(Dict)
Output
Updating an existing value in a Dictionary can be done by using the built-in update() method. Nested
key values can also be added to an existing Dictionary.
Note- While adding a value, if the key-value already exists, the value gets updated otherwise a new
Key with the value is added to the Dictionary.
https://www.geeksforgeeks.org/python-dictionary/ 4/10
11/12/24, 4:54 PM Dictionaries in Python - GeeksforGeeks
The code starts with an empty dictionary and then adds key-value pairs to it. It demonstrates adding
elements with various data types, updating a key’s value, and even nesting dictionaries within the
main dictionary. The code shows how to manipulate dictionaries in Python.
Python
Dict = {}
print("Empty Dictionary: ")
print(Dict)
Dict[0] = 'Geeks'
Dict[2] = 'For'
Dict[3] = 1
print("\nDictionary after adding 3 elements: ")
print(Dict)
Dict['Value_set'] = 2, 3, 4
print("\nDictionary after adding 3 elements: ")
print(Dict)
Dict[2] = 'Welcome'
print("\nUpdated key value: ")
print(Dict)
Dict[5] = {'Nested': {'1': 'Life', '2': 'Geeks'}}
print("\nAdding a Nested Key: ")
print(Dict)
Output
Empty Dictionary:
{}
https://www.geeksforgeeks.org/python-dictionary/ 5/10
11/12/24, 4:54 PM Dictionaries in Python - GeeksforGeeks
The code demonstrates how to access elements in a dictionary using keys. It accesses and prints the
values associated with the keys ‘name’ and 1, showcasing that keys can be of different data types
(string and integer).
Python
Output
There is also a method called get() that will also help in accessing the element from a dictionary. This
method accepts key as argument and returns the value.
The code demonstrates accessing a dictionary element using the get() method. It retrieves and prints
the value associated with the key 3 in the dictionary ‘Dict’ . This method provides a safe way to
access dictionary values, avoiding KeyError if the key doesn’t exist.
Python
https://www.geeksforgeeks.org/python-dictionary/ 6/10
11/12/24, 4:54 PM Dictionaries in Python - GeeksforGeeks
Output
Example: The code works with nested dictionaries. It first accesses and prints the entire nested
dictionary associated with the key ‘Dict1’ . Then, it accesses and prints a specific value by navigating
through the nested dictionaries. Finally, it retrieves and prints the value associated with the
key ‘Name’ within the nested dictionary under ‘Dict2’ .
Python
print(Dict['Dict1'])
print(Dict['Dict1'][1])
print(Dict['Dict2']['Name'])
Output
{1: 'Geeks'}
Geeks
For
The items of the dictionary can be deleted by using the del keyword as given below.
Example: The code defines a dictionary, prints its original content, and then uses the ‘del’ statement
to delete the element associated with key 1. After deletion, it prints the updated dictionary, showing
that the specified element has been removed.
https://www.geeksforgeeks.org/python-dictionary/ 7/10
11/12/24, 4:54 PM Dictionaries in Python - GeeksforGeeks
Python
print("Dictionary =")
print(Dict)
del(Dict[1])
print("Data after deletion Dictionary=")
print(Dict)
Output
Dictionary =
{1: 'Geeks', 'name': 'For', 3: 'Geeks'}
Data after deletion Dictionary=
{'name': 'For', 3: 'Geeks'}
Dictionary Methods
Here is a list of in-built dictionary functions with their description. You can use these functions to
operate on a dictionary.
Method Description
https://www.geeksforgeeks.org/python-dictionary/ 8/10
11/12/24, 4:54 PM Dictionaries in Python - GeeksforGeeks
The code begins with a dictionary ‘dict1’ and creates a copy ‘dict2’ . It then demonstrates several
dictionary operations: clearing ‘dict1’ , accessing values, retrieving key-value pairs and keys,
removing specific key-value pairs, updating a value, and retrieving values. These operations
showcase how to work with dictionaries in Python.
Python
Output
We have covered all about dictionaries in Python, discussed its definition, and uses, and saw different
dictionary methods with examples. The dictionary is an important data structure for storing data in
Python. It is very different from tuples and lists.
https://www.geeksforgeeks.org/python-dictionary/ 10/10