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
* Possible side effects of modifying the original list
625
639
626
-
Python 2.x vs. 3.x
627
-
::::::::::::::::::
640
+
Never use a list comprehension just for its side effects.
628
641
629
-
Starting with Python 3.0, the :py:func:`filter` function returns an iterator instead of a list.
630
-
Wrap it in :py:func:`list` if you truly need a list.
642
+
**Bad**:
631
643
632
644
.. code-block:: python
633
645
634
-
list(filter(...))
646
+
[print(x) for x in seqeunce]
635
647
636
-
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):
648
+
**Good**:
637
649
638
-
* Comprehensions create a new list object.
639
-
* Generators iterate over the original list.
650
+
.. code-block:: python
640
651
641
-
The :py:func:`filter` function:
652
+
for x in sequence:
653
+
print(x)
642
654
643
-
* in 2.x returns a list (use itertools.ifilter if you want an iterator)
644
-
* in 3.x returns an iterator
645
655
646
-
Lists vs. iterators
647
-
:::::::::::::::::::
656
+
Filtering a list
657
+
~~~~~~~~~~~~~~~~
658
+
659
+
**Bad**:
660
+
661
+
Never remove items from a list while you are iterating through it.
662
+
663
+
.. code-block:: python
664
+
665
+
# Filter elements greater than 4
666
+
a = [3, 4, 5]
667
+
for i in a:
668
+
if i >4:
669
+
a.remove(i)
670
+
671
+
Don't make multiple passes through the list.
672
+
673
+
.. code-block:: python
674
+
675
+
while i in a:
676
+
a.remove(i)
677
+
678
+
**Good**:
648
679
649
-
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.
680
+
Use a list comprehension or generator expression.
650
681
651
682
.. code-block:: python
652
683
653
684
# comprehensions create a new list object
654
685
filtered_values = [value for value in sequence if value != x]
655
-
# Or (2.x)
656
-
filtered_values =filter(lambdai: i != x, sequence)
657
686
658
687
# generators don't create another list
659
688
filtered_values = (value for value in sequence if value != x)
660
-
# Or (3.x)
661
-
filtered_values =filter(lambdai: i != x, sequence)
662
-
# Or (2.x)
663
-
filtered_values = itertools.ifilter(lambdai: i != x, sequence)
664
-
665
689
666
690
667
691
Possible side effects of modifying the original list
@@ -673,10 +697,6 @@ Modifying the original list can be risky if there are other variables referencin
673
697
674
698
# replace the contents of the original list
675
699
sequence[::] = [value for value in sequence if value != x]
676
-
# Or
677
-
sequence[::] = (value for value in sequence if value != x)
678
-
# Or
679
-
sequence[::] =filter(lambdavalue: value != x, sequence)
680
700
681
701
682
702
Modifying the values in a list
@@ -705,11 +725,6 @@ It's safer to create a new list object and leave the original alone.
705
725
706
726
# assign the variable "a" to a new list without changing "b"
707
727
a = [i +3for i in a]
708
-
# Or (Python 2.x):
709
-
a =map(lambdai: i +3, a)
710
-
# Or (Python 3.x)
711
-
a =list(map(lambdai: i +3, a))
712
-
713
728
714
729
Use :py:func:`enumerate` keep a count of your place in the list.
0 commit comments