]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41774: Add programming FAQ entry (GH-22402)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 29 Sep 2020 05:11:06 +0000 (22:11 -0700)
committerGitHub <noreply@github.com>
Tue, 29 Sep 2020 05:11:06 +0000 (22:11 -0700)
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>
Doc/faq/programming.rst
Misc/NEWS.d/next/Documentation/2020-09-24-15-35-13.bpo-41774.5IqdGP.rst [new file with mode: 0644]

index 70e9190e5a52058df98cea935e2cdf4ffbb10529..1af04483eb3294b15cc0011ec9903c6b9ed96766 100644 (file)
@@ -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?
 -----------------------------------
 
diff --git a/Misc/NEWS.d/next/Documentation/2020-09-24-15-35-13.bpo-41774.5IqdGP.rst b/Misc/NEWS.d/next/Documentation/2020-09-24-15-35-13.bpo-41774.5IqdGP.rst
new file mode 100644 (file)
index 0000000..af8e024
--- /dev/null
@@ -0,0 +1,2 @@
+In Programming FAQ "Sequences (Tuples/Lists)" section, add "How do you
+remove multiple items from a list".