You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/writing/style.rst
+57-25Lines changed: 57 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -581,21 +581,12 @@ provide a powerful, concise way to work with lists. Also, the :py:func:`map` and
581
581
:py:func:`filter` functions can perform operations on lists using a different,
582
582
more concise syntax.
583
583
584
-
Starting with Python 3.0, the :py:func:`map` and :py:func:`filter`
585
-
functions return an iterator instead of a list. If you really need a list, you
586
-
should wrap these functions in :py:func`list` like so
587
-
588
-
.. code-block:: python
589
-
590
-
list(map(...))
591
-
list(filter(...))
592
-
593
584
Filtering a list
594
585
~~~~~~~~~~~~~~~~
595
586
596
-
**Very Bad**:
587
+
**Bad**:
597
588
598
-
Never remove items from a list that you are iterating over.
589
+
Never remove items from list while you are iterating through it.
599
590
Python will lose track of its current position.
600
591
601
592
.. code-block:: python
@@ -606,28 +597,69 @@ Python will lose track of its current position.
606
597
if i >4:
607
598
a.remove(i)
608
599
600
+
Python has standard ways of filtering lists, but there are several things you need to consider
609
601
610
-
**Bad**:
602
+
* Python 2.x vs. 3.x
603
+
* Lists vs. iterators
604
+
* Creating a new list vs. modifying the original list
605
+
606
+
Python 2.x vs. 3.x
607
+
::::::::::::::::::
608
+
609
+
* Starting with Python 3.0, the :py:func:`map` and :py:func:`filter`
610
+
functions return an iterator instead of a list. If you really need a list, you
611
+
should wrap these functions in :py:func`list` like so
611
612
612
613
.. code-block:: python
613
614
614
-
# Filter elements greater than 4
615
-
a = [3, 4, 5]
616
-
b = []
617
-
for i in a:
618
-
if i >4:
619
-
b.append(i)
615
+
list(map(...))
616
+
list(filter(...))
620
617
621
-
**Good**:
618
+
* List comprehensions and generator expressions work the same in both 2.x and 3.x (except that comprehensions in 2.x "leak" variables into the enclosing namespace)
619
+
620
+
* comprehensions create a new list object
621
+
* generators iterate over the original list
622
+
623
+
* The filter function
624
+
625
+
* in 2.x returns a list (use itertools.ifilter if you want an iterator)
626
+
* in 3.x returns an iterator
627
+
628
+
Lists vs. iterators
629
+
:::::::::::::::::::
630
+
631
+
Creating a new list requires more work and uses more memory. If you a just going to loop through the new list, consider using an iterator instead.
622
632
623
633
.. code-block:: python
624
634
625
-
a = [3, 4, 5]
626
-
b = [i for i in a if i >4]
627
-
# Or (Python 2.x):
628
-
b =filter(lambdax: x >4, a)
629
-
# Or (Python 3.x)
630
-
b =list(filter(lambdax: x >4, a))
635
+
# comprehensions create a new list object
636
+
filtered_values = [value for value in sequence if value != x]
637
+
# Or (2.x)
638
+
filtered_values =filter(lambdai: i != x, sequence)
639
+
640
+
# generators are lazy
641
+
filtered_values = (value for value in sequence if value != x)
642
+
# Or (3.x)
643
+
filtered_values =filter(lambdai: i != x, sequence)
644
+
# Or (2.x)
645
+
filtered_values = itertools.ifilter(lambdai: i != x, sequence)
646
+
647
+
648
+
649
+
Creating a new list vs. modifying the original list
Modifying the original list can be risky if there are other variables referencing it. But you can use *slice assignment* if you really want to do that.
653
+
654
+
.. code-block:: python
655
+
656
+
# replace the contents of the original list
657
+
sequence[::] = [value for value in sequence if value != x]
658
+
# Or
659
+
sequence[::] = (value for value in sequence if value != x)
660
+
# Or
661
+
sequence[::] =filter(lambdavalue: value != x, sequence)
0 commit comments