Introduction

Dictionaries are incredibly useful for storing and retrieving data in Python. They allow us to map unique keys to corresponding values, creating a powerful and easy-to-use reference system. In this tutorial, we will cover the basics of dictionaries in Python, including how to create and manipulate them. We will also explore how to access values stored within a dictionary. By the end of this tutorial, you'll have a solid understanding of how to use dictionaries in your Python programs and be able to incorporate this powerful data type into your future projects. Let's get started!

Table of Contents :

  • What are Dictionaries in Python
    • Creating dictionary using curly brackets
    • Creating dictionary using dict class

Note # : This is just an introductory tutorial on the topic - Dictionary Data type in Python.  We have a full section on Dictionaries in Python in the intermediate section of this course. That section consists of a number of tutorials that cover this topic in full detail, explaining various Dictionary operations as well.  After going through this tutorial if you feel like studying python dictionaries in detail, you'd like to go through our Dictionaries in Python section.

What are Dictionaries in Python

  • In Python, dictionaries are the unordered collections of elements.
  • In dictionaries elements are stored in Key-Value pairs. 
  • The dictionary data type is implemented using a  dict  class in Python.
  • A dictionary cannot have duplicate keys.
  • A dictionary can have duplicate values. 
  • A simple use case of dict data type can be storing names of employees along with the employee ids.
  • The employee-ids being unique will serve as keys and names will be the values.
  • If we try to duplicate a key, the old value of that key will be replaced by the new one.
  • A dictionary value can be of any data type i.e. strings, list, tuple etc. 
  • An object can be used as a key in a dictionary only if it is hashable.
  • The dictionary is a mutable data type
  • A dictionary can be created in two ways in Python
    • By enclosing key-values pairs within curly brackets {}
    • Creating instance of dict() class.

Creating dictionary using curly brackets :

  • We can create a dictionary by enclosing the key-value pairs within curly brackets.
  • Code Sample :

employees = {
    1: "John",
    2: "Peter",
    3: "John",
    4: "Alice"
}

print("employees is a dictionary.")
print(f"Data Type of employees is {type(employees)}")
print(f"Value of employees = ")
print(employees)

# Output
# employees is a dictionary.
# Data Type of employees is <class 'dict'>
# Value of employees = 
# {1: 'John', 2: 'Peter', 3: 'John', 4: 'Alice'}


Creating dictionary using dict class :

  • We can create a dictionary by instantiating  dict  class.
  • Code Sample :

employees = dict([(1, "John"), (2, "Peter"), (3, "John"),(4, "Alice")])

print("employees is a dictionary.")
print(f"Data Type of employees is {type(employees)}")
print(f"Value of employees = ")
print(employees)

# Output
# employees is a dictionary.
# Data Type of employees is <class 'dict'>
# Value of employees = 
# {1: 'John', 2: 'Peter', 3: 'John', 4: 'Alice'}


Prev. Tutorial : Range data type

Next Tutorial : Sets data type