From a11c0d44c1946b6466a0cede640ca656e8a95fa7 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Sat, 20 Jan 2018 19:46:44 +0530 Subject: [PATCH] difference --- README.md | 18 ++++++++++++++++++ snippets/compact.md | 1 + snippets/deep_flatten.md | 1 + snippets/difference.md | 16 ++++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 snippets/difference.md diff --git a/README.md b/README.md index 76bf7fc8d..3afa85392 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Use `filter()` to filter out falsey values (False, None, 0, and ""). ```python + def compact(arr): return list(filter(lambda x: bool(x), arr)) @@ -116,12 +117,29 @@ def spread(arg): + ``` ```python deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5] ``` +### difference + +Returns the difference between two arrays. + +Create a `set` from `b`, then use list comprehension to only keep values not contained in `b` + +```python + +def difference(a, b): + b = set(b) + return [item for item in a if item not in b] + +``` +``` python +difference([1, 2, 3], [1, 2, 4]) # [3] +``` ### gcd Calculates the greatest common divisor between two or more numbers/lists. diff --git a/snippets/compact.md b/snippets/compact.md index 477001202..0c3c43c4b 100644 --- a/snippets/compact.md +++ b/snippets/compact.md @@ -6,6 +6,7 @@ Use `filter()` to filter out falsey values (False, None, 0, and ""). ```python + def compact(arr): return list(filter(lambda x: bool(x), arr)) diff --git a/snippets/deep_flatten.md b/snippets/deep_flatten.md index 4f3d76b7b..b4ac9e6da 100644 --- a/snippets/deep_flatten.md +++ b/snippets/deep_flatten.md @@ -26,6 +26,7 @@ def spread(arg): + ``` ```python diff --git a/snippets/difference.md b/snippets/difference.md new file mode 100644 index 000000000..32c8db200 --- /dev/null +++ b/snippets/difference.md @@ -0,0 +1,16 @@ +### difference + +Returns the difference between two arrays. + +Create a `set` from `b`, then use list comprehension to only keep values not contained in `b` + +```python + +def difference(a, b): + b = set(b) + return [item for item in a if item not in b] + +``` +``` python +difference([1, 2, 3], [1, 2, 4]) # [3] +``` \ No newline at end of file