diff options
author | Terry Jan Reedy <tjreedy@udel.edu> | 2020-09-29 05:02:44 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-29 05:02:44 (GMT) |
commit | 5b0181d1f6474c2cb9b80bdaf3bc56a78bf5fbe7 (patch) | |
tree | c6c26e72dfac72aab59be5f9366b3cb5a0b259b3 /Doc/faq | |
parent | b0dfc7581697f20385813582de7e92ba6ba0105f (diff) | |
download | cpython-5b0181d1f6474c2cb9b80bdaf3bc56a78bf5fbe7.zip cpython-5b0181d1f6474c2cb9b80bdaf3bc56a78bf5fbe7.tar.gz cpython-5b0181d1f6474c2cb9b80bdaf3bc56a78bf5fbe7.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".
Diffstat (limited to 'Doc/faq')
-rw-r--r-- | Doc/faq/programming.rst | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index 4f4ea8b..76ae4d2 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? ----------------------------------- |