[Pythonmac-SIG] Duplicate items in a list

Andres Corrada andres@corrada.com
Mon, 09 Aug 1999 09:24:12 -0400


Hi Pieter,

	I won't vouch for my understanding and use of the copy module, but here is
some code that deletes duplicates from a list (not tuple):

import copy

def DeleteDuplicates( list ):

	if len( list ) <= 1:
		return copy.copy( list )
	else:
		return ListJoin( [ list[ 0 ] ], 
						 RemoveAll( list[ 0 ], DeleteDuplicates( list[ 1: ] ) ) )

def ListJoin( listA, listB ):

	returnList = []

	for i in range( len( listA ) ):
		returnList.append( listA[ i ] )

	for i in range( len( listB ) ):
		returnList.append( listB[ i ] )

	return returnList

def RemoveAll( objectToRemove, list ):

	returnList = copy.copy( list )

	while 1:
		try:
			returnList.remove( objectToRemove )
		except:
			break

	return returnList

-------------------------------------------------------
Andres Corrada-Emmanuel    Email: andres@corrada.com
Owner                      http://www.corrada.com/mamey
Mamey                      Phone: (413) 587-9595
-------------------------------------------------------