Open In App

Dictionaries in Python

Improve
Improve
Like Article
Like
Save
Share
Report

A dictionary in Python is a data structure that stores the value in key:value pairs.

Example:

As you can see from the example, data is stored in key:value pairs in dictionaries, which makes it easier to find values.

Python3




Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print(Dict)


Output:

{1: 'Geeks', 2: 'For', 3: 'Geeks'}

Python Dictionary Syntax

dict_var = {key1 : value1, key2 : value2, …..}

What is a Python 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.

How to Create a Dictionary

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.

Python3




Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
 
Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)


Output

Dictionary with the use of Integer Keys: 
{1: 'Geeks', 2: 'For', 3: 'Geeks'}
Dictionary with the use of Mixed Keys:
{'Name': 'Geeks', 1: [1, 2, 3, 4]}

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{}. 

Different Ways to Create a Python Dictionary

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.

Python3




Dict = {}
print("Empty Dictionary: ")
print(Dict)
 
Dict = dict({1: 'Geeks', 2: 'For', 3: 'Geeks'})
print("\nDictionary with the use of dict(): ")
print(Dict)
 
Dict = dict([(1, 'Geeks'), (2, 'For')])
print("\nDictionary with each item as a pair: ")
print(Dict)


Output:

Empty Dictionary: 
{}
Dictionary with the use of dict():
{1: 'Geeks', 2: 'For', 3: 'Geeks'}
Dictionary with each item as a pair:
{1: 'Geeks', 2: 'For'}

Complexities for Creating a Dictionary:

  • Time complexity: O(len(dict))
  • Space complexity: O(n)

Nested Dictionaries

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.

Python3




Dict = {1: 'Geeks', 2: 'For',
        3: {'A': 'Welcome', 'B': 'To', 'C': 'Geeks'}}
 
print(Dict)


Output:

{1: 'Geeks', 2: 'For', 3: {'A': 'Welcome', 'B': 'To', 'C': 'Geeks'}}

More on Python Nested Dictionary

Adding Elements to a Dictionary

The addition of elements can be done in multiple ways. One value at a time can be added to a Dictionary by defining value along with the key e.g. Dict[Key] = ‘Value’.

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.

Example: Add Items to a Python Dictionary with Different DataTypes

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.

Python3