"Collapsing" a list into a list of changes

Alan McIntyre alan.mcintyre at esrgtech.com
Sat Feb 5 08:54:41 EST 2005


Tony,

Actually I only want to remove a certain kind of duplication; if an item 
occurs twice - say like this: [1,1,1,2,2,2,1,1,1], then I need to keep 
the order and occurrence of the individual values: [1,2,1].  Using a 
dict as you proposed loses the order of occurrence, as well as multiple 
occurrences of groups of the same item.

If I didn't need those two qualities of the list to be preserved, 
though, I think I'd use something like your solution (if I was using a 
Python older than 2.3) or Steve Coats' solution posted above using Set.

Thanks!
Alan

Tony wrote:
> Here is a version using dictionary properties, ie no duplication of
> keys.
> 
> def condense(l):
>  d={}
>  for item in l:
>     d[item]=1
>  l=d.keys()
>  return l
> 
> Cheers
> Tony
> 



More information about the Python-list mailing list