# basics variables

Modifying List and Dictionary Data

Lists are mutable — you can change an item by assigning to its index, or add a new item to the end with .append():

fruits = ["apple", "banana", "cherry"]
fruits[0] = "apricot"
fruits.append("date")
print(fruits)

prints:

['apricot', 'banana', 'cherry', 'date']

Dictionaries work the same way — assign to a key to update it, or assign to a new key to add it:

ages = {"Sam": 10, "Zoe": 12}
ages["Sam"] = 11
ages["Max"] = 9
print(ages)

prints:

{'Sam': 11, 'Zoe': 12, 'Max': 9}

Your turn: a list colors and a dictionary scores are given. Change colors[0] to "orange", then use .append() to add "purple" to the end. Add a new entry to scores: "Max" mapped to 9. Print colors, then print scores.

💡 need a hint?

pyb-var-modify-list-dict.py🔒 given lines are locked — write your code in between
loading...