Python Dictionaries
Python Dictionaries

Python Dictionaries

Dictionary are unordered collections of unique values stored in (Key-Value) pairs. Learn how to create, access, and modify a dict in Python and all other operations we can perform on a dictionary

In Python version 3.7 and onwards, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

Python dictionary represents a mapping between a key and a value. In simple terms, a Python dictionary can store pairs of keys and values. Each key is linked to a specific value. Once stored in a dictionary, you can later obtain the value using just the key.

For example, consider the Phone lookup, where it is very easy and fast to find the phone number(value) when we know the name(Key) associated with it.

image

The following are the properties of a dictionary.

  1. Mutable: Dictionaries can be modified after creation—you can add, update, or remove key-value pairs at any time.
  2. Ordered (Python 3.7+): Dictionaries maintain the insertion order of key-value pairs, so items are returned in the order they were added.
  3. Heterogeneous: Dictionaries can contain keys and values of different types—strings, integers, booleans, lists, or even other dictionaries.
  4. Unique Keys: Keys in a dictionary must be unique. If the same key is assigned more than once, the most recent value will overwrite the previous one.

Why use a dictionary?

  • Dictionaries are ideal for storing data that needs to be looked up by a label or identifier, rather than by position.
  • They provide fast access to values through keys using hashing, making retrieval very efficient.
  • Their ability to store diverse data types as values increases their flexibility.
  • The unique key constraint ensures that each piece of data is clearly defined and not accidentally overwritten unless explicitly intended.
  • Their mutable nature makes them perfect for dynamic datasets where changes are frequent.

Table of contents

  1. Characteristics of dictionaries
  2. Creating a dictionary
    • Empty Dictionary
  3. Accessing elements of a dictionary
    • Get all keys and values
    • Iterating a dictionary
  4. Find a length of a dictionary
  5. Adding items to the dictionary
    • Set default value to a key
  6. Removing items from the dictionary
  7. Checking if a key exists
  8. Join two dictionary
    • Using update() method
    • Using **kwargs to unpack
    • Join two dictionaries having few items in common
  9. Copy a Dictionary
    • Copy using the assignment operator
  10. Nested dictionary
    • Add multiple dictionaries inside a single dictionary

Creating a Dictionary

Accessing elements of a dictionary

Find a length of a dictionary

Adding items to the dictionary

Removing items from the dictionary

Checking if a Key Exists in a Python Dictionary

Merging Dictionaries in Python

Copy a Dictionary

Nested dictionary

Sort Dictionary

Dictionary Comprehension

Python Builtin Functions with Dictionary

When to Use Dictionaries in Python