Update frequencies.md
This commit is contained in:
committed by
GitHub
parent
2da7bcd612
commit
6d52040e37
@ -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.
|
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 `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
|
```py
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
def frequencies(lst):
|
def frequencies(lst):
|
||||||
freq = defaultdict(int)
|
freq = defaultdict(int)
|
||||||
for val in lst:
|
for val in lst:
|
||||||
freq[val]+=1
|
freq[val] += 1
|
||||||
return dict(freq)
|
return dict(freq)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user