Python Tutorials
Basic

Data Structure and Data Types

String

List

Tuple

Set

Dictionary

Functions / Methods

Advance

Exception Handling

Python Dictionary


Dictionary

A Python dictionary stores data in key-value pairs.

The dictionary collection is ordered (The items have a defined order, and that order will not change.).

The dictionary items are changeable and do not allow duplicates.

You can create a dictionary using curly ({}) brackets, but items are in the key-value pairs.

Example
company_dictionary = {
    "name": "Google", 
    "country": "USA", 
    "start_year": 1995
}

print(company_dictionary)
{'name': 'Google', 'country': 'USA', 'start_year': 1995}

In the above example, we have created a dictionary with a key-value pair within curly ({}) brackets.

In dictionay, "name", "country" and "start_year" are keys, and "Google", "USA" and "1995" are values respectively.


Access Dictionary Item Value Using Key

You can access a dictionary item value using its key.

Example
company_dictionary = {
    "name": "Google", 
    "country": "USA", 
    "start_year": 1995
}

print(company_dictionary["start_year"])
1995

We are accessing the start_year of a company by using the key of the dictionary, so as in the output, you can see the starting year of the company is 1995.


Dictionary Not Allow Duplicate Items

Duplicate keys are not allowed in the Python dictionary, but if you put them, it considers the last key-value pair.

Example
company_dictionary = {
    "name": "Google", 
    "country": "USA", 
    "start_year": 1995, 
    "start_year": 2000
}

print(company_dictionary["start_year"])
2000

In the company_dictionary, the start_year we added two times, but when we access the starting year of the company, we get the second added year.


Python Dictionary Items Data Types

For dictionary items, you can set any data type.

Example
company_dictionary = {
    "name": "Google", 
    "country": "USA", 
    "start_year": 1995, 
    "is_private": True, 
    "products": ["Android OS", "Chrome Browser", "Google Maps", "Google Search Engine", "Youtube"]
}

print(company_dictionary)
{'name': 'Google', 'country': 'USA', 'start_year': 1995, 'is_private': True, 'products': ['Android OS', 'Chrome Browser', 'Google Maps', 'Google Search Engine', 'Youtube']}

In the company_dictionary, we set different data type items such as string, boolean, integer, and list. As you can see in the output, there is no error after setting different data types in the dictionary.


Dictionary Items Count

To determine how many items are in the dictionary, use the len() function.

Example
company_dictionary = {
    "name": "Google", 
    "country": "USA", 
    "start_year": 1995
}

print(len(company_dictionary))
3

In the above example, the number of items count is 3.


Check Dictionary Data Type

If you want to check the data type of the dictionary, then use the type() function.

Example
company_dictionary = {
    "name": "Google", 
    "country": "USA", 
    "start_year": 1995
}

print(type(company_dictionary))
<'class' 'dict'>

As in the output, the data type of the dictionary is <"class" "dict"> (dictionary).