Skip to content

Commit d50a070

Browse files
bpo-41774: Add programming FAQ entry (GH-22402)
In the "Sequences (Tuples/Lists)" section, add "How do you remove multiple items from a list". (cherry picked from commit 5b0181d) Co-authored-by: Terry Jan Reedy <[email protected]>
1 parent e400840 commit d50a070

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Doc/faq/programming.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,21 @@ This converts the list into a set, thereby removing duplicates, and then back
11631163
into a list.
11641164

11651165

1166+
How do you remove multiple items from a list
1167+
--------------------------------------------
1168+
1169+
As with removing duplicates, explicitly iterating in reverse with a
1170+
delete condition is one possibility. However, it is easier and faster
1171+
to use slice replacement with an implicit or explicit forward iteration.
1172+
Here are three variations.::
1173+
1174+
mylist[:] = filter(keep_function, mylist)
1175+
mylist[:] = (x for x in mylist if keep_condition)
1176+
mylist[:] = [x for x in mylist if keep_condition]
1177+
1178+
If space is not an issue, the list comprehension may be fastest.
1179+
1180+
11661181
How do you make an array in Python?
11671182
-----------------------------------
11681183

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In Programming FAQ "Sequences (Tuples/Lists)" section, add "How do you
2+
remove multiple items from a list".

0 commit comments

Comments
 (0)