×
   ❮   
PYTHON FOR DJANGO DJANGO FOR BEGINNERS DJANGO SPECIFICS Roadmap
     ❯   

VARIABLES & TYPES IN PYTHON

Mapping Type

Understanding Python Dictionaries

Introduction

A dictionary in Python, denoted as dict, is a built-in data type that allows you to store an unordered collection of key-value pairs. Each key is unique, and each key is associated with a value. Dictionaries are mutable, meaning they can be modified after creation, making them an essential tool for managing data in Python.

Creating a Dictionary

You can create a dictionary by enclosing key-value pairs in curly braces {}, with each pair separated by a comma. The key and value are separated by a colon :.

Example:

my_dict = {"name": "Alice", "age": 25}

In this example, the dictionary my_dict has two key-value pairs: "name" with the value "Alice", and "age" with the value 25.

Accessing Values in a Dictionary

To access the value associated with a specific key, you can use square brackets [] containing the key.

Example:

name = my_dict["name"]
print(name)

Output:

Alice

Here, the value associated with the key "name" is accessed and stored in the variable name, which is then printed.

Adding or Updating Entries in a Dictionary

One of the powerful features of dictionaries is the ability to add new key-value pairs or update existing ones. You can do this by assigning a value to a key using the assignment operator =. If the key already exists, its value will be updated; otherwise, a new key-value pair will be added.

Example:

my_dict["age"] = 26
my_dict["city"] = "New York"
print(my_dict)

Output:

{"name": "Alice", "age": 26, "city": "New York"}

In this example, the value associated with the key "age" is updated from 25 to 26, and a new key-value pair "city": "New York" is added to the dictionary.

Removing Entries from a Dictionary

You can remove a key-value pair from a dictionary using the pop() method, which takes the key as an argument and removes the corresponding entry from the dictionary. The value associated with the key is returned.

Example:

age = my_dict.pop("age")
print(age)
print(my_dict)

Output:

26
{"name": "Alice", "city": "New York"}

In this example, the key-value pair with the key "age" is removed from the dictionary, and its value 26 is printed. The modified dictionary, without the "age" key, is also printed.

Iterating Through a Dictionary

Dictionaries allow you to easily iterate through their keys, values, or both. You can use a for loop to iterate over the keys, values, or key-value pairs in a dictionary.

Iterating Through Keys:

for key in my_dict:
    print(key)

Output:

name
city

Iterating Through Values:

for value in my_dict.values():
    print(value)

Output:

Alice
New York

Iterating Through Key-Value Pairs:

for key, value in my_dict.items():
    print(f"{key}: {value}")

Output:

name: Alice
city: New York

In these examples, different aspects of the dictionary my_dict are printed: first the keys, then the values, and finally both keys and values together in key-value pairs.