mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00: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 5b0181d1f6
)
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
This commit is contained in:
parent
cca896e13b
commit
868c8e41eb
2 changed files with 17 additions and 0 deletions
|
@ -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?
|
||||
-----------------------------------
|
||||
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
In Programming FAQ "Sequences (Tuples/Lists)" section, add "How do you
|
||||
remove multiple items from a list".
|
Loading…
Add table
Add a link
Reference in a new issue