-2

I have a list containing the date and a specific code. I need to return a list with all the duplicated codes except the most recent one. For example:

    full_list = ["06-01-22------aa", "06-02-22------aa", "06-02-22------bb", "06-03-22------bb", "06-03-22------cc"]

The desired list should return:

    desired_list = ["06-01-22------aa", "06-02-22------bb"]
Ferote
  • 1
  • 1
    Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please [see here](http://stackoverflow.com/help/how-to-ask) to learn how to write effective questions. – Yevhen Kuzmovych Aug 22 '22 at 14:26
  • Also, see https://stackoverflow.com/questions/7961363/removing-duplicates-in-lists and https://stackoverflow.com/questions/18169965/how-to-delete-last-item-in-list – Yevhen Kuzmovych Aug 22 '22 at 14:28
  • please show what you have tried and where you are stuck. – D.L Aug 22 '22 at 14:36

1 Answers1

0

Sorry for my faux pas. I was able to figure it out in case it helps another poor soul like me.

for k in full_list:
        code = k[-2:]
        count = sum(code in s for s in full_list)
        if count > 1:
            duplicates_temp = [i for i in full_list if code in i]
            duplicates_temp.remove(max(duplicates_temp))
            duplicates_list.extend(duplicates_temp)
            full_list= [x for x in full_list if x not in duplicates_temp]
            duplicates_temp = []

Now, my question is, is this the fastest, most efficient way to do it? Thanks!

Ferote
  • 1