del and editing a list in place

The third UPI row was a duplicate. You may replace it by index or drop it. del is the laptop spelling; here you rebuild keepers so the source file still exists as evidence.

Market shopping list

A list holds many items together.

You will be able to

  • Replace a bad row by index when the slot must stay
  • Drop a row by rebuilding a keepers list (del on a laptop)
  • Leave the source evidence intact when an audit is likely

The third UPI row was a duplicate

Someone pasted the canteen txn twice. You may overwrite that slot by index if the file layout is fixed — rows[2] = the correction. Or you may drop it. On a laptop, del rows[2] removes that slot and shifts the tail. This sandbox practises the two honest twins: assign a new dict at an index, or walk the source and append keepers to a new list.

Deleting while you walk the same list is how internships skip a row by accident. Rebuild keepers. The source file on disk still exists as evidence until a laptop write replaces it.

In-place versus a new table

In-place means the same list object changes: a replacement at an index, or a laptop del. A new table means keepers.append. Prefer a new table when the principal may still ask “what did the export actually contain at 09:00?” Prefer in-place when you are editing a scratch list you just built and nobody else holds a reference.

Never “delete” by writing 0 into amount and hoping reports skip zeros. That is how PF lines turn into fake rupees.

Laptop: del drops a slot

rows = ["UPI-A", "UPI-B", "UPI-A"]
del rows[2]
print(rows)
rows[0] = "UPI-C"
print(rows)

Words that matter

del
Laptop spelling that removes a slot (or a name).
In-place
Mutate this list object — replace by index, or del.
Keepers
A new list of rows you still want.

Common mistakes

Avoid: Removing items from a list while a for-loop is walking that same list.

Do this: Build a keepers list, or replace one index you already computed.

Run it step by step

Each box is a real program. Press Run, change a number, Run again — the output must follow your code.

1. Step 1 — replace by index

Slot 2 was a duplicate A. Overwrite with C.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

2. Step 2 — drop by rebuilding keepers

Keep unique UPI ids. Second A is skipped. Keepers length is 2.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Example program — Correct a PF line, keep the sheet length

Index 1 was 0 by mistake. Replace. Three rows remain.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — Fix slot 1

rows is A, A, B. Replace index 1 with C. Print so C appears.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. del on a laptop…
2. When an audit may still need the 09:00 export, prefer…

Progress is stored in a browser cookie on this device.