mirror of
https://github.com/kennethreitz/python-guide.git
synced 2026-06-05 23:00:18 +00:00
Remove explicit recommendation to use map and filter over list comprehensions
This commit is contained in:
@@ -809,16 +809,12 @@ and can be used as a key for a dictionary.
|
|||||||
|
|
||||||
One peculiarity of Python that can surprise beginners is that
|
One peculiarity of Python that can surprise beginners is that
|
||||||
strings are immutable. This means that when constructing a string from
|
strings are immutable. This means that when constructing a string from
|
||||||
its parts, it is much more efficient to accumulate the parts in a list,
|
its parts, appending each part to the string is inefficient because
|
||||||
which is mutable, and then glue ('join') the parts together when the
|
the entirety of the string is copied on each append.
|
||||||
full string is needed. One thing to notice, however, is that list
|
Instead, it is much more efficient to accumulate the parts in a list,
|
||||||
comprehensions are better and faster than constructing a list in a loop
|
which is mutable, and then glue (``join``) the parts together when the
|
||||||
with calls to ``append()``.
|
full string is needed. List comprehensions are usually the fastest and
|
||||||
|
most idiomatic way to do this.
|
||||||
One other option is using the map function, which can 'map' a function
|
|
||||||
('str') to an iterable ('range(20)'). This results in a map object,
|
|
||||||
which you can then ('join') together just like the other examples.
|
|
||||||
The map function can be even faster than a list comprehension in some cases.
|
|
||||||
|
|
||||||
**Bad**
|
**Bad**
|
||||||
|
|
||||||
@@ -830,7 +826,7 @@ The map function can be even faster than a list comprehension in some cases.
|
|||||||
nums += str(n) # slow and inefficient
|
nums += str(n) # slow and inefficient
|
||||||
print nums
|
print nums
|
||||||
|
|
||||||
**Good**
|
**Better**
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
@@ -840,20 +836,12 @@ The map function can be even faster than a list comprehension in some cases.
|
|||||||
nums.append(str(n))
|
nums.append(str(n))
|
||||||
print "".join(nums) # much more efficient
|
print "".join(nums) # much more efficient
|
||||||
|
|
||||||
**Better**
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# create a concatenated string from 0 to 19 (e.g. "012..1819")
|
|
||||||
nums = [str(n) for n in range(20)]
|
|
||||||
print "".join(nums)
|
|
||||||
|
|
||||||
**Best**
|
**Best**
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
# create a concatenated string from 0 to 19 (e.g. "012..1819")
|
# create a concatenated string from 0 to 19 (e.g. "012..1819")
|
||||||
nums = map(str, range(20))
|
nums = [str(n) for n in range(20)]
|
||||||
print "".join(nums)
|
print "".join(nums)
|
||||||
|
|
||||||
One final thing to mention about strings is that using ``join()`` is not always
|
One final thing to mention about strings is that using ``join()`` is not always
|
||||||
|
|||||||
Reference in New Issue
Block a user