Skip to content

Commit e69d7e7

Browse files
authored
2.x vs 3.x, lists vs. iterators
Describe the differences between 2.x and 3.x and suggest situations when iterators would be more appropriate than lists.
1 parent 436dec8 commit e69d7e7

File tree

1 file changed

+57
-25
lines changed

1 file changed

+57
-25
lines changed

docs/writing/style.rst

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -581,21 +581,12 @@ provide a powerful, concise way to work with lists. Also, the :py:func:`map` and
581581
:py:func:`filter` functions can perform operations on lists using a different,
582582
more concise syntax.
583583

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-
593584
Filtering a list
594585
~~~~~~~~~~~~~~~~
595586

596-
**Very Bad**:
587+
**Bad**:
597588

598-
Never remove items from a list that you are iterating over.
589+
Never remove items from list while you are iterating through it.
599590
Python will lose track of its current position.
600591

601592
.. code-block:: python
@@ -606,28 +597,69 @@ Python will lose track of its current position.
606597
if i > 4:
607598
a.remove(i)
608599
600+
Python has standard ways of filtering lists, but there are several things you need to consider
609601

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
611612

612613
.. code-block:: python
613614
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(...))
620617
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.
622632

623633
.. code-block:: python
624634
625-
a = [3, 4, 5]
626-
b = [i for i in a if i > 4]
627-
# Or (Python 2.x):
628-
b = filter(lambda x: x > 4, a)
629-
# Or (Python 3.x)
630-
b = list(filter(lambda x: 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(lambda i: 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(lambda i: i != x, sequence)
644+
# Or (2.x)
645+
filtered_values = itertools.ifilter(lambda i: i != x, sequence)
646+
647+
648+
649+
Creating a new list vs. modifying the original list
650+
:::::::::::::::::::::::::::::::::::::::::::::::::::
651+
652+
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(lambda value: value != x, sequence)
662+
631663
632664
Modifying the values in a list
633665
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)