# Demonstration of list and dictionary modifications in Python
# Topics: insert, update, delete, and move/reorder

print("=== LIST MODIFICATIONS ===")

# Start with a list
courses = ["Math", "History", "Science"]
print("\nOriginal list:", courses)

# INSERT: add an item at a specific position
courses.insert(1, "Art")
print("\nAfter insert('Art' at index 1):", courses)

# UPDATE: change an existing item by index
courses[2] = "World History"
print("\nAfter update index 2 to 'World History':", courses)

# DELETE: remove by value
courses.remove("Science")
print("\nAfter remove('Science'):", courses)

# DELETE from the middle by index using del (no returned value)
del courses[1]
print("\nAfter del courses[1] (middle item):", courses)

# ADD to end (another common insert style)
courses.append("Physics")
print("\nAfter append('Physics'):", courses)

# MOVE/REORDER: move an item from one index to another
item_to_move = courses.pop(0)  # remove first item and store it
courses.insert(2, item_to_move)  # insert it at a new position
print("\nAfter moving first item to index 2:", courses)

# DELETE by index
deleted_item = courses.pop(1)
print("\nAfter pop(1), removed:", deleted_item)
print("\nCurrent list:", courses)

# Small stack demo: push and pop on the end of a list
stack = [10, 20, 30, 40]
print("\nStack start:", stack)
stack.append(50)  # push
print("After push (append 50):", stack)
top_item = stack.pop()  # pop
print("After pop(), removed:", top_item, "| Stack now:", stack)


print("\n=== DICTIONARY MODIFICATIONS ===")

# Start with a dictionary
student = {
    "name": "Alex",
    "major": "Computer Science",
    "gpa": 3.4
}
print("Original dictionary:", student)

# INSERT: add a new key-value pair
student["level"] = "sophomore"
print("\nAfter insert key 'level':", student)

# UPDATE: change the value of an existing key
student["gpa"] = 3.7
print("\nAfter update 'gpa' to 3.7:", student)

# UPDATE multiple values at once with update()
student.update({"major": "Data Science", "graduation_year": 2028})
print("\nAfter update() for multiple keys:", student)

# DELETE: remove by key with del
del student["level"]
print("\nAfter del student['level']:", student)

# DELETE safely using pop (returns removed value)
removed_value = student.pop("graduation_year")
print("\nAfter pop('graduation_year'), removed:", removed_value)
print("Current dictionary:", student)

# MOVE/REORDER (dictionary order):
# In Python 3.7+, dictionaries keep insertion order.
# We can move a key to the end by popping and reassigning it.
name_value = student.pop("name")
student["name"] = name_value
print("\nAfter moving 'name' key to end:", student)

print("\n=== LIST OF DICTIONARIES (COMBINED DEMO) ===")

students = [
    {"name": "Ana", "major": "Math", "gpa": 3.5},
    {"name": "Ben", "major": "History", "gpa": 3.2},
    {"name": "Cara", "major": "Biology", "gpa": 3.8}
]
print("Starting list of dictionaries:", students)

# INSERT a new dictionary into the middle of the list
students.insert(1, {"name": "Diego", "major": "CS", "gpa": 3.9})
print("\nAfter insert new student at index 1:", students)

# UPDATE a value inside one dictionary
students[2]["major"] = "World History"
print("\nAfter updating index 2 major:", students)

# DELETE a dictionary from the middle of the list
del students[1]
print("\nAfter del students[1] (middle record):", students)

# MOVE a dictionary record to a new position
moved_student = students.pop(0)
students.insert(2, moved_student)
print("\nAfter moving first student to index 2:", students)

print("\nDemo complete.")
