2

I have a list

[<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>, 
<Storage {'CaseID': 46L, 'PatientProfile_ID': 2L}>,
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>,
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>, 
<Storage {'CaseID': 46L, 'PatientProfile_ID': 2L}>, 
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>, 
<Storage {'CaseID': 46L, 'PatientProfile_ID': 2L}>, 
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>, 
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>, 
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>, 
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>, 
<Storage {'CaseID': 46L, 'PatientProfile_ID': 2L}>, 
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>]

I want the distinct value of this Expected Result is

[
 <Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>, 
 <Storage {'CaseID': 46L, 'PatientProfile_ID': 2L}>
]

This data is being pulled from Database. Please don't advice me to use the DISTINCT keyword on the database. Its been sorted by another column. Using distinct will remove the sorted result.

Can it be done in python. Or do i have to write a for loop to do the same thing?

MyCODE

entries=db.query("SELECT cases.ID as CaseID,PatientProfile_ID FROM \
                             `callog` ,consultation,cases WHERE callog.Consultation_ID=consultation.ID and consultation.Case_ID = cases.ID and \
                             Doctor_ID="+str(Id)+" ORDER BY callog.CallDate DESC")
            rows=entries.list()
            return rows

3 Answers3

1

This should handle things nicely.

def remove_dupes_keep_order(seq):
  seen = set()
  seen_add = seen.add
  return [ x for x in seq if not (x in seen or seen_add(x))]

Using seen_add speeds up the operation.

See also, this SO question.

Since the issue seems to be that your items are of type Storage, try this:

def remove_dupes_keep_order(seq):
  seen = set()
  seen_add = seen.add
  return [ x for x in seq if not (json.dumps(x, sort_keys=True) in seen or seen_add(json.dumps(x, sort_keys=True)))]
Richard
  • 56,349
  • 34
  • 180
  • 251
0

Try this:

newList = list(set(oldList))

above, turns your list into a set (which removes duplicates) and then back into a list.

For more information: Refer this. See, if that helps.

Gaurav Dave
  • 6,838
  • 9
  • 25
  • 39
0

After all the trial and error, I am back to For loops

def remove_dupes_keep_order(self,seq):
        Lister=[]
        for row in seq:
            Dicter={"CaseID":row["CaseID"],"PatientProfile_ID":row["PatientProfile_ID"]}
            Lister.append(Dicter)
        seen = []
        seen_add = seen.append
        return [ x for x in Lister if not (x in seen or seen_add(x))]

Snippet Courtesy: @Richard