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>I think that in this case the creation of a new list is more efficient and clear in comparison with the permutations in the original list:</p>

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

def duplicate(alist):

    filtered, duplicates = [], []
    for element in alist:
        if element in filtered:
            duplicates.append(element)
            continue
        filtered.append(element)

    if not duplicates:
        print "No duplicate item found"
        return alist
    return filtered + duplicates

new_list = duplicate(testlist)
print new_list
</code></pre>

python python-2.7 scripting