Reword some articles

This commit is contained in:
Chalarangelo
2021-11-07 17:34:40 +02:00
parent 4670439e56
commit b8c2ed5479
11 changed files with 43 additions and 43 deletions

View File

@ -6,7 +6,7 @@ authors: maciv,chalarangelo
cover: blog_images/walking-on-top.jpg
excerpt: Writing short, efficient Python code is not always straightforward. Read how we optimize our list snippets to increase performance using a couple of simple tricks.
firstSeen: 2020-03-15T12:50:05+02:00
lastUpdated: 2021-06-12T19:30:41+03:00
lastUpdated: 2021-11-07T16:34:37+03:00
---
Writing short and efficient Python code is not always easy or straightforward. However, it's often that we see a piece of code and we don't realize the thought process behind the way it was written. We will be taking a look at the [difference](/python/s/difference) snippet, which returns the difference between two iterables, in order to understand its structure.
@ -18,7 +18,7 @@ def difference(a, b):
return [item for item in a if item not in b]
```
The above implementation may work well enough, but doesn't account for duplicates in `b`, making the code take more time than necessary in cases with many duplicates in the second list. To solve this issue, we can make use of the `set()` method, which will only keep the unique values in the list:
This implementation may work well enough, but doesn't account for duplicates in `b`. This makes the code take more time than necessary in cases with many duplicates in the second list. To solve this issue, we can make use of the `set()` method, which will only keep the unique values in the list:
```py
def difference(a, b):
@ -50,7 +50,7 @@ def difference(a, b):
return [item for item in a if item not in _b]
```
Another option worth mentioning when analyzing performance for this snippet is the use of a list comprehension versus using something like `filter()` and `list()`. Implementing the same code using the latter option would result in something like this:
Another option worth mentioning in terms of performance is the use of a list comprehension versus `filter()` and `list()`. Implementing the same code using the latter option would result in something like this:
```py
def difference(a, b):
@ -58,6 +58,6 @@ def difference(a, b):
return list(filter(lambda item: item not in _b, a))
```
Using `timeit` to analyze the performance of the last two code examples, it's pretty clear that using list comprehension can be up to ten times faster than the alternative, as it's a native language feature that works very similar to a simple `for` loop without the overhead of the extra function calls. This explains why we prefer it, apart from readability.
Using `timeit` to analyze the performance of the last two code examples, it's pretty clear that using list comprehension can be up to ten times faster than the alternative. This is due to it being a native language feature that works very similar to a simple `for` loop without the overhead of the extra function calls. This explains why we prefer it, apart from readability.
This pretty much applies to most mathematical list operation snippets, such as [difference](/python/s/difference), [symmetric_difference](/python/s/symmetric-difference) and [intersection](/python/s/intersection).