What are Dictionaries in Python?

Python dictionary is an unordered collection of items. Each item of a dictionary has a key/value pair.

Dictionaries are optimized to retrieve values when the key is known.

Creating a dictionary is as simple as placing items inside curly braces {} separated by commas.

An item has a key and a corresponding value that is expressed as a pair (key: value).


# empty dictionary
 my_dict = {} 

# dictionary with integer keys
my_dict = {1: 'apple', 2: 'ball'} 

# dictionary with mixed keys 
my_dict = {'name': 'John', 1: [2, 4, 3]} 

# using dict() 
my_dict = dict({1:'apple', 2:'ball'}) 

# from sequence having each item as a pair
my_dict = dict([(1,'apple'), (2,'ball')])

As you can see from above, we can also create a dictionary using the built-in dict() function.