Skip to content
Filter a shared list without skipping items or losing aliases

Filter a shared list without skipping items or losing aliases

November 19, 2026

Removing matching entries while iterating a list can skip adjacent matches. A comprehension avoids that shifting-index problem. If other code holds a reference to the same list, assign the filtered contents through a full slice to preserve the object it observes.

Set up

Tested on Linux with CPython 3.12.14. Use Bash, Python 3.12 or newer, a text editor, and the mktemp, rm and rmdir utilities. No packages or network access are needed. Keep the same terminal open and stop if setup fails. Run without -O so the assertions remain enabled.

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save as filter_shared_list.py:

jobs = ["expired-a", "expired-b", "ready"]
for job in jobs:
    if job.startswith("expired-"):
        jobs.remove(job)
assert jobs == ["expired-b", "ready"]
print("Removing during iteration:", jobs)

jobs = ["expired-a", "expired-b", "ready"]
observer = jobs
jobs = [job for job in jobs if not job.startswith("expired-")]
assert observer == ["expired-a", "expired-b", "ready"]
print("Rebound list:", jobs)
print("Old alias:", observer)

jobs = ["expired-a", "expired-b", "ready"]
observer = jobs
jobs[:] = [job for job in jobs if not job.startswith("expired-")]
assert observer is jobs
assert observer == ["ready"]
print("Slice-assigned alias:", observer)
print("Same object:", observer is jobs)

for values in ([], ["expired-a"], ["ready", "ready"]):
    alias = values
    expected = [value for value in values if not value.startswith("expired-")]
    values[:] = expected
    assert values is alias and values == expected

Run:

python3 -B filter_shared_list.py

Observed output:

Removing during iteration: ['expired-b', 'ready']
Rebound list: ['ready']
Old alias: ['expired-a', 'expired-b', 'ready']
Slice-assigned alias: ['ready']
Same object: True

Build the filtered contents before replacing them

In the first loop, removing the first expired entry shifts the next one into its place. The iterator advances past that shifted entry, so it survives. This incomplete result is the intentional failure case.

Rebinding the variable to a comprehension’s result produces the correct filtered list, but the old alias still refers to the original object. Full-slice assignment replaces the original object’s contents. The identity assertion confirms that the observer and the working variable still refer to one list.

The mutable-sequence reference describes replacing a slice with the contents of an iterable. Here, the entire comprehension finishes before slice assignment starts, so the filtering loop reads unchanged list positions.

Decide whether aliases should see the change

Use a new list when callers should retain their old collection. Use slice assignment when changing the shared collection is part of the API contract. Both forms preserve the order and duplicates of retained entries; the final assertions cover an empty list, removal of every entry, and repeated retained values.

The comprehension allocates a temporary list. Its predicate should not mutate the source while filtering, and shared access from other threads needs separate coordination. Retained elements are references to the same objects, so this does not deep-copy mutable records. The example uses strings and one thread.

Clean up

Remove the saved file and empty scratch directory. If the directory is not empty, inspect its contents before removing anything else.

rm -- filter_shared_list.py
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

Acknowledgements

This post was created with assistance from AI.

Last updated on