P
P
PILITYXXX1232020-08-11 10:01:57
Python
PILITYXXX123, 2020-08-11 10:01:57

How to get only duplicate elements from a list in Python?

It is necessary to write out repeated values ​​from the list - ['test', 'test1', 'test', 'test', 'test2', 'test2']. That is, the result should be - ['test', test2]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pcdesign, 2020-08-11
@PILITYXXX123

>>> t =  ['test', 'test1', 'test', 'test', 'test2', 'test2']
>>> import collections
>>> print([item for item, count in collections.Counter(t).items() if count > 1])
['test', 'test2']

0
0xD34F, 2020-08-11
@0xD34F

list(set(n for n in arr if arr.count(n) > 1))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question