From a5124253f1a81606ea96a24492911fb74b7711f6 Mon Sep 17 00:00:00 2001 From: Victor Schmidt Date: Mon, 8 Oct 2018 17:56:23 +0200 Subject: [PATCH 1/2] use set() --- snippets/difference.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/snippets/difference.md b/snippets/difference.md index 949a26e75..0ae57c7c4 100644 --- a/snippets/difference.md +++ b/snippets/difference.md @@ -2,11 +2,12 @@ Returns the difference between two iterables. -Use list comprehension to only keep values not contained in `b` +Use list comprehension to only keep values not contained in `b`. Use set to test `in` in `O(1)` time. ```python def difference(a, b): - return [item for item in a if item not in b] + _b = set(b) + return [item for item in a if item not in _b] ``` ``` python difference([1, 2, 3], [1, 2, 4]) # [3] From 2574fa0c6b2bd6ce85028222b46c24e693ab899a Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 19 Aug 2019 14:02:59 +0300 Subject: [PATCH 2/2] Update difference.md --- snippets/difference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/difference.md b/snippets/difference.md index 0ae57c7c4..21d09c5c1 100644 --- a/snippets/difference.md +++ b/snippets/difference.md @@ -2,7 +2,7 @@ Returns the difference between two iterables. -Use list comprehension to only keep values not contained in `b`. Use set to test `in` in `O(1)` time. +Use list comprehension to only keep values not contained in `b`. ```python def difference(a, b):