From 8911146de18b19b35c4f1ba9435bcdbc5ddb46de Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Sat, 4 Jan 2020 14:29:42 +0100 Subject: [PATCH 1/4] Improve filter functions --- snippets/filter_non_unique.md | 8 ++++++-- snippets/filter_unique.md | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/snippets/filter_non_unique.md b/snippets/filter_non_unique.md index b9dd96923..6563285a0 100644 --- a/snippets/filter_non_unique.md +++ b/snippets/filter_non_unique.md @@ -5,11 +5,15 @@ tags: list,beginner Filters out the non-unique values in a list. -Use list comprehension and `list.count()` to create a list containing only the unique values. +Use `Counter` to get the count of each value in the list. +Use list comprehension to create a list containing only the unique values. ```py +from collections import Counter + def filter_non_unique(lst): - return [item for item in lst if lst.count(item) == 1] + counter = Counter(lst) + return [item for item, count in counter.items() if count == 1] ``` ```py diff --git a/snippets/filter_unique.md b/snippets/filter_unique.md index 92184cf0b..7632cd494 100644 --- a/snippets/filter_unique.md +++ b/snippets/filter_unique.md @@ -5,11 +5,15 @@ tags: list,beginner Filters out the unique values in a list. -Use list comprehension and `list.count()` to create a list containing only the non-unique values. +Use `Counter` to get the count of each value in the list. +Use list comprehension to create a list containing only the non-unique values. ```py +from collections import Counter + def filter_unique(lst): - return [x for x in set(item for item in lst if lst.count(item) > 1)] + counter = Counter(lst) + return [item for item, count in counter.items() if count > 1] ``` ```py From 23f68fee3ade3e7ce0a5ea0a620a40640c789731 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 5 Jan 2020 14:38:31 +0200 Subject: [PATCH 2/4] Update filter_unique.md --- snippets/filter_unique.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/snippets/filter_unique.md b/snippets/filter_unique.md index 7632cd494..5cc3ed8f8 100644 --- a/snippets/filter_unique.md +++ b/snippets/filter_unique.md @@ -12,8 +12,7 @@ Use list comprehension to create a list containing only the non-unique values. from collections import Counter def filter_unique(lst): - counter = Counter(lst) - return [item for item, count in counter.items() if count > 1] + return [item for item, count in Counter(lst).items() if count > 1] ``` ```py From 949a29df4f30101bda07a9deb309f424a391c584 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 5 Jan 2020 14:39:06 +0200 Subject: [PATCH 3/4] Update filter_non_unique.md --- snippets/filter_non_unique.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/snippets/filter_non_unique.md b/snippets/filter_non_unique.md index 6563285a0..373d1edbc 100644 --- a/snippets/filter_non_unique.md +++ b/snippets/filter_non_unique.md @@ -5,15 +5,14 @@ tags: list,beginner Filters out the non-unique values in a list. -Use `Counter` to get the count of each value in the list. +Use a `Counter` to get the count of each value in the list. Use list comprehension to create a list containing only the unique values. ```py from collections import Counter def filter_non_unique(lst): - counter = Counter(lst) - return [item for item, count in counter.items() if count == 1] + return [item for item, count in counter = Counter(lst).items() if count == 1] ``` ```py From ded0927bad0a42a3f13d52c0e4501f9e515f60cc Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 5 Jan 2020 14:39:21 +0200 Subject: [PATCH 4/4] Update filter_unique.md --- snippets/filter_unique.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/filter_unique.md b/snippets/filter_unique.md index 5cc3ed8f8..5f5431514 100644 --- a/snippets/filter_unique.md +++ b/snippets/filter_unique.md @@ -5,7 +5,7 @@ tags: list,beginner Filters out the unique values in a list. -Use `Counter` to get the count of each value in the list. +Use a `Counter` to get the count of each value in the list. Use list comprehension to create a list containing only the non-unique values. ```py