Update filter_unique.md
This commit is contained in:
@ -5,13 +5,13 @@ tags: list,beginner
|
|||||||
|
|
||||||
Filters out the unique values in a list.
|
Filters out the unique values in a list.
|
||||||
|
|
||||||
Use list comprehension and `list.count()` to create a list containing only the unique values.
|
Use list comprehension and `list.count()` to create a list containing only the non-unique values.
|
||||||
|
|
||||||
```py
|
```py
|
||||||
def filter_unique(lst):
|
def filter_unique(lst):
|
||||||
return [item for item in lst if lst.count(item) == 1]
|
return [x for x in set(item for item in lst if lst.count(item) > 1)]
|
||||||
```
|
```
|
||||||
|
|
||||||
```py
|
```py
|
||||||
filter_unique([1, 2, 2, 3, 4, 4, 5]) # [1, 3, 5]
|
filter_unique([1, 2, 2, 3, 4, 4, 5]) # [2, 4]
|
||||||
```
|
```
|
||||||
Reference in New Issue
Block a user