Answer the question
In order to leave comments, you need to log in
Why aren't all elements of the list removed?
I'm trying to solve a problem on checkio. All unique elements must be removed. For some reason, when entering a list with all unique elements, such an output is obtained.
def checkio(data):
for i in data:
if data.count(i) < 2:
data.remove(i)
return data
checkio([1, 2, 3, 4, 5]) # >>> [2, 4]
Answer the question
In order to leave comments, you need to log in
import copy
def checkio(data):
xdata = copy.deepcopy(data)
for i in data:
if (xdata.count(i) < 2):
xdata.remove(i)
return xdata
From a performance point of view, this will be faster:
from collections import Counter
def checkio(data):
c = Counter(data)
return [i for i in data if c[i] > 1]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question