8000 bpo-41774: Add programming FAQ entry (GH-22402) · python/cpython@868c8e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 868c8e4

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 <tjreedy@udel.edu>
1 parent cca896e commit 868c8e4

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
@@ -1164,6 +1164,21 @@ This converts the list into a set, thereby removing duplicates, and then back
11641164
into a list.
11651165

11661166

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

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)
0