Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: Dict Editing

check

Setting and removing dictionary values.


In Python, you can modify dictionaries by adding new key-value pairs, updating existing pairs, or removing pairs altogether.

Adding and Updating Dictionary Values

You can add a new key-value pair or update an existing pair by assigning a value to a key.

user = {
  "name": "Bob",
  "email": "[email protected]",
  "age": 35
}
user["age"] = 36  # updating
user["address"] = "123 Main St"  # adding
print(user)

> {"name": "Bob", "email": "[email protected]", "age": 36, "address": "123 Main St"}

Removing Dictionary Values

You can remove a key-value pair from a dictionary with the del keyword.

user = {
  "name": "Bob",
  "email": "[email protected]",
  "age": 35,
  "address": "123 Main St"
}
del user["address"]
print(user)

> {"name": "Bob", "email": "[email protected]", "age": 35}

Practice: Editing a Dictionary

  1. Create a dictionary student with keys "name", "id", and "course" and assign appropriate values to each key.

  2. Add a new key-value pair "age": 21 to the dictionary.

  3. Update the value of the key "course" to "Advanced Data Science".

  4. Remove the key-value pair with the key "id".

  5. Print the updated dictionary.

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> {"name": "Alice", "course": "Advanced Data Science", "age": 21}
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
student = {
  "name": "Alice",
  "id": 123,
  "course": "Data Science"
}
student["age"] = 21
student["course"] = "Advanced Data Science"
del student["id"]
print(student)

Practice: Editing a Complex Dictionary

Let's consider a dictionary courses representing a catalog of courses and their corresponding students. Each key in the dictionary is a course name and the value is a list of student IDs.

  1. Create the courses dictionary with at least three courses and at least two students in each course.

  2. Add a new student to the "Data Science" course with the append() method.

  3. Remove a student from the "Web Development" course with the remove() method.

  4. Print the updated courses dictionary.

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> {"Data Science": [111, 112, 118], "Web Development": [114, 115], "Design": [116, 117]}
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
courses = {
  "Data Science": [111, 112],
  "Web Development": [113, 114, 115],
  "Design": [116, 117]
}
courses["Data Science"].append(118)
courses["Web Development"].remove(113)
print(courses)

Well done! Being able to edit dictionaries is a crucial part of working with data in Python.