[Python-checkins] bpo-41774: Add programming FAQ entry (GH-22402)

Miss Islington (bot) webhook-mailer at python.org
Tue Sep 29 01:27:15 EDT 2020

commit: 868c8e41eb1d7dc032679ae06e62c0d60edd7725
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-09-28T22:27:06-07:00

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 at udel.edu>

A Misc/NEWS.d/next/Documentation/2020-09-24-15-35-13.bpo-41774.5IqdGP.rst
M Doc/faq/programming.rst

diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst
index 66d210a55fac7..1435765063f73 100644
--- a/Doc/faq/programming.rst
+++ b/Doc/faq/programming.rst
@@ -1164,6 +1164,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
index 0000000000000..af8e02437cb2b
--- /dev/null
+++ b/Misc/NEWS.d/next/Documentation/2020-09-24-15-35-13.bpo-41774.5IqdGP.rst
@@ -0,0 +1,2 @@
+In Programming FAQ "Sequences (Tuples/Lists)" section, add "How do you
+remove multiple items from a list".

More information about the Python-checkins mailing list