Imrove frequencies snippet implementation (#234)

Co-authored-by: Isabelle Viktoria Maciohsek <maciv@hotmail.gr>
This commit is contained in:
Azan Bin Zahid
2020-10-03 18:50:18 +05:00
committed by GitHub
parent 64fe8f8a4d
commit 9e76e8f67f

View File

@ -5,16 +5,12 @@ tags: list,intermediate
Returns a dictionary with the unique values of a list as keys and their frequencies as the values.
- Use a `for` loop to populate a dictionary, `f`, with the unique values in `lst` as keys, adding to existing keys every time the same value is encountered.
- Use `list.count()` to get the frequency 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 functools import reduce
def frequencies(lst):
f = {}
for x in lst:
f[x] = f[x] + 1 if x in f else 1
return f
return dict((k, lst.count(k)) for k in lst)
```
```py