File tree Expand file tree Collapse file tree 1 file changed +12
-4
lines changed
Expand file tree Collapse file tree 1 file changed +12
-4
lines changed Original file line number Diff line number Diff line change @@ -586,8 +586,7 @@ Filtering a list
586586
587587**Bad **:
588588
589- Never remove items from list while you are iterating through it.
590- Python will lose track of its current position.
589+ Never remove items from a list while you are iterating through it.
591590
592591.. code-block :: python
593592
@@ -597,8 +596,17 @@ Python will lose track of its current position.
597596 if i > 4 :
598597 a.remove(i)
599598
599+ Don't make multiple passes through the list.
600+
601+ .. code-block :: python
602+
603+ while i in a:
604+ a.remove(i)
605+
606+ **Good **:
607+
600608Python has a few standard ways of filtering lists.
601- You will need to consider
609+ The approach you use depends on
602610
603611* Python 2.x vs. 3.x
604612* Lists vs. iterators
@@ -608,7 +616,7 @@ Python 2.x vs. 3.x
608616::::::::::::::::::
609617
610618Starting with Python 3.0, the :py:func: `filter ` function returns an iterator instead of a list.
611- If you really need a list, you should wrap it in :py:func: `list ` like so
619+ Wrap it in :py:func: `list ` if you truly need a list.
612620
613621.. code-block :: python
614622
You can’t perform that action at this time.
0 commit comments