From 2da7bcd61276989e1ee0a95024a2f905c1415426 Mon Sep 17 00:00:00 2001 From: guru kiran <47276342+gurukiran07@users.noreply.github.com> Date: Wed, 14 Oct 2020 19:34:31 +0530 Subject: [PATCH 1/2] Updated frequencies.md, Resolves#362 --- snippets/frequencies.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/snippets/frequencies.md b/snippets/frequencies.md index 91dc18eda..13657b0b9 100644 --- a/snippets/frequencies.md +++ b/snippets/frequencies.md @@ -5,12 +5,16 @@ tags: list,intermediate Returns a dictionary with the unique values of a list as keys and their frequencies as the values. -- Use `list.count()` to get the frequency of each unique element. +- Use `collections.defaultdict()` to store the frequencies of each unique element. - Use `dict()` constructor to return a dictionary with the unique elements of the list as keys and their frequencies as the values. ```py +from collections import defaultdict def frequencies(lst): - return dict((k, lst.count(k)) for k in lst) + freq = defaultdict(int) + for val in lst: + freq[val]+=1 + return dict(freq) ``` ```py From 6d52040e3746d81275bea1f659f94e398dfa5d38 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Fri, 16 Oct 2020 20:37:50 +0300 Subject: [PATCH 2/2] Update frequencies.md --- snippets/frequencies.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/snippets/frequencies.md b/snippets/frequencies.md index 13657b0b9..f78fe75b5 100644 --- a/snippets/frequencies.md +++ b/snippets/frequencies.md @@ -6,14 +6,15 @@ tags: list,intermediate Returns a dictionary with the unique values of a list as keys and their frequencies as the values. - Use `collections.defaultdict()` to store the frequencies of each unique element. -- Use `dict()` constructor to return a dictionary with the unique elements of the list as keys and their frequencies as the values. +- Use `dict()` to return a dictionary with the unique elements of the list as keys and their frequencies as the values. ```py from collections import defaultdict + def frequencies(lst): freq = defaultdict(int) for val in lst: - freq[val]+=1 + freq[val] += 1 return dict(freq) ```