summaryrefslogtreecommitdiffstats
path: root/Doc/faq/programming.rst
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2020-09-29 05:11:06 (GMT)
committerGitHub <noreply@github.com>2020-09-29 05:11:06 (GMT)
commitd50a0700265536a20bcce3fb108c954746d97625 (patch)
treeae8c3d38da4698ee1e0fd4dec4a5417f148c3482 /Doc/faq/programming.rst
parente4008404fbc0002c49becc565d42e93eca11dd75 (diff)
downloadcpython-d50a0700265536a20bcce3fb108c954746d97625.zip
cpython-d50a0700265536a20bcce3fb108c954746d97625.tar.gz
cpython-d50a0700265536a20bcce3fb108c954746d97625.tar.bz2
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 5b0181d1f6474c2cb9b80bdaf3bc56a78bf5fbe7) Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Diffstat (limited to 'Doc/faq/programming.rst')
-rw-r--r--Doc/faq/programming.rst15
1 files changed, 15 insertions, 0 deletions
diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst
index 70e9190..1af0448 100644
--- a/Doc/faq/programming.rst
+++ b/Doc/faq/programming.rst
@@ -1163,6 +1163,21 @@ This converts the list into a set, thereby removing duplicates, and then back
into a list.
+How do you remove multiple items from a list
+--------------------------------------------
+
+As with removing duplicates, explicitly iterating in reverse with a
+delete condition is one possibility. However, it is easier and faster
+to use slice replacement with an implicit or explicit forward iteration.
+Here are three variations.::
+
+ mylist[:] = filter(keep_function, mylist)
+ mylist[:] = (x for x in mylist if keep_condition)
+ mylist[:] = [x for x in mylist if keep_condition]
+
+If space is not an issue, the list comprehension may be fastest.
+
+
How do you make an array in Python?
-----------------------------------