Dictionary: Creating, Accessing Values, Adding, Modifying and Deleting Items in Dictionary

Dictionary is a collection of data stored in key-value pairs. Dictionaries are enclosed within curly braces { } and are mutable, meaning their contents can be changed after creation.

Features of Dictionary:

  1. Stores data as key-value pairs.
  2. Mutable in nature.
  3. Keys must be unique.
  4. Values can be duplicated.
  5. Elements are accessed using keys.
  6. Dictionaries are dynamic and can grow or shrink.

Example

student = {
    "name": "Kaif",
    "age": 20,
    "course": "Python"
}

Creating a Dictionary:

Method 1: Using Curly Braces

student = {
    "name": "Kaif",
    "age": 20,
    "course": "Python"
}

print(student)

Output

{'name': 'Kaif', 'age': 20, 'course': 'Python'}

Method 2: Using dict() Function

student = dict(name="Kaif", age=20, course="Python")

print(student)

Output

{'name': 'Kaif', 'age': 20, 'course': 'Python'}
Method 3: Creating an Empty Dictionary
student = {}

print(student)

Output

{}
Accessing Values in a Dictionary:

Dictionary values are accessed using their keys.

Using Square Brackets

Example

student = {
    "name": "Kaif",
    "age": 20,
    "course": "Python"
}

print(student["name"])
print(student["age"])

Output

Kaif
20

Using get() Method

The get() method returns the value of the specified key.

Example

student = {
    "name": "Kaif",
    "age": 20
}

print(student.get("name"))

Output

Kaif

Advantage

If the key does not exist, get() returns None instead of generating an error.

print(student.get("city"))

Output

None

Adding Items to a Dictionary:

New key-value pairs can be added by assigning a value to a new key.

Syntax

dictionary[key] = value

Example

student = {
    "name": "Kaif",
    "age": 20
}

student["city"] = "Mumbai"

print(student)

Output

{'name': 'Kaif', 'age': 20, 'city': 'Mumbai'}

Modifying Items in a Dictionary:

Existing values can be changed by assigning a new value to an existing key.

Example

student = {
    "name": "Kaif",
    "age": 20
}

student["age"] = 21

print(student)

Output

{'name': 'Kaif', 'age': 21}

Characteristics

  1. Only the value changes.
  2. Key remains the same.
  3. Dictionary is modified directly.

Deleting Items from a Dictionary:

Items can be removed using different methods.

1. del Statement

Removes a specific key-value pair.

Example

student = {
    "name": "Kaif",
    "age": 20,
    "city": "Mumbai"
}

del student["city"]

print(student)

Output

{'name': 'Kaif', 'age': 20}

2. pop() Method

Removes and returns the value of a specified key.

Example

student = {
    "name": "Kaif",
    "age": 20
}

age = student.pop("age")

print(age)
print(student)

Output

20
{'name': 'Kaif'}

3. popitem() Method

Removes and returns the last inserted key-value pair.

Example

student = {
    "name": "Kaif",
    "age": 20,
    "city": "Mumbai"
}

student.popitem()

print(student)

Output

{'name': 'Kaif', 'age': 20}

4. clear() Method

Removes all items from the dictionary.

Example

student = {
    "name": "Kaif",
    "age": 20
}

student.clear()

print(student)

Output

{}
Summary Table
Operation Syntax/Method Purpose
Create Dictionary {} or dict() Creates a dictionary
Access Value dict[key] Retrieves value
Safe Access get() Retrieves value safely
Add Item dict[key] = value Adds new key-value pair
Modify Item dict[key] = new_value Changes existing value
Delete Item del dict[key] Removes item
Remove Item pop() Removes and returns value
Remove Last Item popitem() Removes last inserted pair
Delete All Items clear() Empties dictionary

Advantages of Dictionary:

  1. Fast data retrieval using keys.
  2. Easy to add, update, and delete items.
  3. Stores data in key-value format.
  4. Suitable for structured data storage.
  5. Supports nested dictionaries.

Leave a Reply

error: Content is protected !!