listsal2 = [1,2,2,3,3,4,5,6,7,8]
listsal3 = 
counter = 0
for i in listsal2:
item = listsal2.count(i)
if item > 1:
counter = item
while counter > 1:
counter = counter - 1
In your case, even after you account for
i=2 once, you go through the list again for 2, since it exists multiple times. This is why 2 and 3 end up in
listsal3 twice. Instead, what you want to do is only go through the list once for each unique item.
listsal2 = [1,2,2,3,3, 4] newlist = set(listsal2) listsal3 =  counter = 0 for i in newlist: item = listsal2.count(i) if item > 1: counter = item print counter while counter > 1: listsal3.append(i) counter = counter - 1 print listsal2, listsal3
To get the unique items from your list, convert it to a set.
Another way to do it: Simply keep a list containing the counts of each unique element, take the maximum of it, and trace back to the element that it corresponds to.
newset = set(listsal2) newlist = list(set) counts =  for item in newlist: counts.append(listsal2.count(item)) maxcount = max(counts) max_occurring_item = newlist[counts.index(maxcount)]