How do I find the duplicates in a list and create another list with them?
To remove duplicates use set(a)
. To print duplicates, something like:
a = [1,2,3,2,1,5,6,5,5,5]
import collections
print([item for item, count in collections.Counter(a).items() if count > 1])
## [1, 2, 5]
Note that Counter
is not particularly efficient (timings) and probably overkill here. set
will perform better. This code computes a list of unique elements in the source order:
seen = set()
uniq = []
for x in a:
if x not in seen:
uniq.append(x)
seen.add(x)
or, more concisely:
seen = set()
uniq = [x for x in a if x not in seen and not seen.add(x)]
I don't recommend the latter style, because it is not obvious what not seen.add(x)
is doing (the set add()
method always returns None
, hence the need for not
).
To compute the list of duplicated elements without libraries:
seen = set()
dupes = []
for x in a:
if x in seen:
dupes.append(x)
else:
seen.add(x)
or, more concisely:
seen = set()
dupes = [x for x in a if x in seen or seen.add(x)]
If list elements are not hashable, you cannot use sets/dicts and have to resort to a quadratic time solution (compare each with each). For example:
a = [[1], [2], [3], [1], [5], [3]]
no_dupes = [x for n, x in enumerate(a) if x not in a[:n]]
print no_dupes # [[1], [2], [3], [5]]
dupes = [x for n, x in enumerate(a) if x in a[:n]]
print dupes # [[1], [3]]
How to find the duplicates in a list and create another list with them ? with answer
Using collections.Counter
:
>>> list1 = [1,2,3,4,4,5,5,5,6,6,7,8,8,8,8]
>>> from collections import Counter
>>> [(i, count) for i, count in Counter(list1).items() if count > 1]
[(4, 2), (5, 3), (6, 2), (8, 4)]
Identify duplicate values in a list in Python
These answers are O(n), so a little more code than using mylist.count()
but much more efficient as mylist
gets longer
If you just want to know the duplicates, use collections.Counter
from collections import Counter
mylist = [20, 30, 25, 20]
[k for k,v in Counter(mylist).items() if v>1]
If you need to know the indices,
from collections import defaultdict
D = defaultdict(list)
for i,item in enumerate(mylist):
D[item].append(i)
D = {k:v for k,v in D.items() if len(v)>1}
Find duplicate values in list, add them and move the result to another list
Just use a counter:
from collections import Counter
start_list = [5,5,5,5,6,6,8,8]
c = Counter(start_list)
print([x*n for x, n in c.items()])
How to print only the duplicate elements in python list
There is the Counter
class from collections
that does the trick
from collections import Counter
lst = [4,3,2,4,5,6,4,7,6,8]
d = Counter(lst) # -> Counter({4: 3, 6: 2, 3: 1, 2: 1, 5: 1, 7: 1, 8: 1})
res = [k for k, v in d.items() if v > 1]
print(res)
# [4, 6]
How do I find the duplicates in a list and create another list with them?
To remove duplicates use set(a)
. To print duplicates, something like:
a = [1,2,3,2,1,5,6,5,5,5]
import collections
print([item for item, count in collections.Counter(a).items() if count > 1])
## [1, 2, 5]
Note that Counter
is not particularly efficient (timings) and probably overkill here. set
will perform better. This code computes a list of unique elements in the source order:
seen = set()
uniq = []
for x in a:
if x not in seen:
uniq.append(x)
seen.add(x)
or, more concisely:
seen = set()
uniq = [x for x in a if x not in seen and not seen.add(x)]
I don't recommend the latter style, because it is not obvious what not seen.add(x)
is doing (the set add()
method always returns None
, hence the need for not
).
To compute the list of duplicated elements without libraries:
seen = set()
dupes = []
for x in a:
if x in seen:
dupes.append(x)
else:
seen.add(x)
or, more concisely:
seen = set()
dupes = [x for x in a if x in seen or seen.add(x)]
If list elements are not hashable, you cannot use sets/dicts and have to resort to a quadratic time solution (compare each with each). For example:
a = [[1], [2], [3], [1], [5], [3]]
no_dupes = [x for n, x in enumerate(a) if x not in a[:n]]
print no_dupes # [[1], [2], [3], [5]]
dupes = [x for n, x in enumerate(a) if x in a[:n]]
print dupes # [[1], [3]]
Add the duplicate in another list
you can use dict to store and filter for dupe values
from collections import defaultdict
res = defaultdict(list)
for item in values:
_, color, food = item
res[color].append(item)
res[food].append(item)
[v for v in res.values() if len(v) > 1]
result:
[[[1, 'red1', 'food4'], [1, 'red1', 'food9']],
[[1, 'red1', 'food4'], [1, 'red2', 'food4']]]
Related Topics
Create a Pandas Dataframe by Appending One Row At a Time
How to Explicitly Free Memory in Python
Urllib and "Ssl: Certificate_Verify_Failed" Error
"Unicode Error "Unicodeescape" Codec Can't Decode Bytes... Cannot Open Text Files in Python 3
What Is the Eafp Principle in Python
How to Pass a Variable Between Flask Pages
Import a Module from a Relative Path
How to Expand the Output Display to See More Columns of a Pandas Dataframe
Why Does List.Append Evaluate to False in a Boolean Context
Convert List of Dictionaries to a Pandas Dataframe
How to Read and Write CSV Files With Python
Loop "Forgets" to Remove Some Items