From 1e16cd52981072c76d8f1c02a828e4a42f1f752b Mon Sep 17 00:00:00 2001 From: Helmut Irle Date: Sat, 10 Oct 2020 23:28:47 +0200 Subject: [PATCH 1/2] Use Python's built-in count method to do the counting for us. --- snippets/count_occurences.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/count_occurences.md b/snippets/count_occurences.md index f75b24376..b68696d52 100644 --- a/snippets/count_occurences.md +++ b/snippets/count_occurences.md @@ -5,11 +5,11 @@ tags: list,beginner Counts the occurrences of a value in a list. -- Increment a counter for every item in the list that has the given value and is of the same type. +- Use the `count` method of Python's list object to count the number of occurrences. ```py def count_occurrences(lst, val): - return len([x for x in lst if x == val and type(x) == type(val)]) + return lst.count(val) ``` ```py From 88dceb260a3adb69873303e4bacd0ea7da8f0a95 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Sun, 11 Oct 2020 10:29:24 +0300 Subject: [PATCH 2/2] Update count_occurences.md --- snippets/count_occurences.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/count_occurences.md b/snippets/count_occurences.md index b68696d52..f6f8c8269 100644 --- a/snippets/count_occurences.md +++ b/snippets/count_occurences.md @@ -5,7 +5,7 @@ tags: list,beginner Counts the occurrences of a value in a list. -- Use the `count` method of Python's list object to count the number of occurrences. +- Use `list.count()` to count the number of occurrences of `val` in `lst`. ```py def count_occurrences(lst, val):