Append duplicate items at the end of list whithout changing the order
<p>I am new to python and was trying to Append duplicate items at the end of list whithout changing the order</p>

<pre><code>testlist = [1, 2, 32, 8, 1, 17, 5, 2, 42, 13, 56]

def duplicate(alist):
    p = len(alist)
    duplicate = False
    for i in range(0, p):
        for j in range (i + 1, p):
            if alist[i] == alist[j]:
                b = alist.index(alist[j])
                a = alist.pop(b)
                alist.append(a)
                p -= 1
                duplicate = True

    print alist

if duplicate == False:
    print "No duplicate item found"

duplicate(testlist)
</code></pre>

<p>OUTPUT : <code>[32, 8, 1, 17, 5, 2, 42, 13, 56, 1, 2]</code></p>

<p>DESIRED OUTPUT : <code>[1, 2, 32, 8, 17, 5, 42, 13, 56, 1, 2]</code></p>

<p>Any help what wrong I am doing here</p>

<p>Instead of checking values, compare on index.</p>

<p>Please check this code:</p>

<pre><code>testlist = [1, 2, 32, 8, 1, 17, 5, 2, 42, 13, 56,32]

print("Original list - ",testlist)
tmp_list = []
exclude =[]
for i in range(len(testlist)):
    if i == testlist.index(testlist[i]):

        tmp_list.append(testlist[i])
    else:
        exclude.append(testlist[i])
tmp_list.extend(exclude)
testlist = tmp_list

print("Updated list - ",testlist)
</code></pre>

<p>Output:</p>

<pre><code>C:\Users\dinesh_pundkar\Desktop&gt;python c.py
Original list -  [1, 2, 32, 8, 1, 17, 5, 2, 42, 13, 56, 32]
Updated list -  [1, 2, 32, 8, 17, 5, 42, 13, 56, 1, 2, 32]

C:\Users\dinesh_pundkar\Desktop&gt;
</code></pre>

python python-2.7 scripting