[Tutor] List processing question - consolidating duplicateentries

Tiger12506 keridee at jayco.net
Wed Nov 28 13:43:32 CET 2007


> s=set()
> [s.add(tuple(x)) for x in myEntries]
> myEntries = [list(x) for x in list(s)]

This could be written more concisely as...

s = set(tuple(x) for x in myEntries)
myEntries = [list(x) for x in list(s)]

Generator expressions are really cool.

Not what the OP asked for exactly. He wanted to eliminate duplicates by 
adding their last columns. He said the last column is a number of hours that 
pertain to the first four columns. When you apply your method, it will not 
get rid of duplicate projects, just circumstances where the number of hours 
is equivalent also, and of course, not add the hours like they should be. I 
like the dictionary approach for this personally...

di = {}
for x in myEntries:
    try: di[x[:3]]+=x[4]
    except KeyError: di[x[:3]] = x[4]

This can be written even smaller and cleaner if you use the default value 
method... 



More information about the Tutor mailing list