Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8541861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:55:34+00:00 2026-06-11T11:55:34+00:00

Possible Duplicate: List of dictionaries, eliminating duplicates of one key, sorting by another Does

  • 0

Possible Duplicate:
List of dictionaries, eliminating duplicates of one key, sorting by another

Does anyone have any clever idea how to remove duplicates from my list of dictionary objects, where two keys should compare to equals and a third is a timestamp? The object that is to be left in the list should be the one with the latest timestamp.

To remove any ambiguity from my question I present a simple test case for my goal:

from datetime import datetime
now = datetime.now()
future = datetime(now.year + 100, *now.timetuple()[1:-2])

# Elements are considered equal if A and B matches.
data = [{"A":10,"B":20,"D":now}, \
        {"A":45,"B":20,"D":now}, \
        {"A":45,"B":20,"D":future}, \
        {"A":66,"B":6,"D":future}, \
        {"A":66,"B":6,"D":now}]

def make_unique(lst):
    pass

make_unique(data)

# data should now contain: (10,20,now),(45,20,future),(66,6,future)
print(data)

Performance is not terrible important, but the list could very well contain a couple of thousends of elements.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:55:36+00:00Added an answer on June 11, 2026 at 11:55 am

    You could try to create a dictionary unique and iterate on data. Make the tuple (d['A'], d['B']) your key and the corresponding item d your value. Then, it’s a matter of checking whether (1) the key is already in the dictionary and (2) if the corresponding time stamp is more recent than the one stored:

    unique = {}
    for d in data:
        key = (d['A'],d['B'])
        if key in unique:
            if unique[key]['D'] < d['D']:
                unique[key] = d
        else:
            unique[key] = d
    

    Then, just retrieve an unsorted list as unique.values().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Does std::list::remove method call destructor of each removed element? Assume I have
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
Possible Duplicate: How to sum dict elements I have a list of dictionaries as
Possible Duplicate: list.clear() vs list = new ArrayList<int>(); I have a list: List<Integer> l1
Possible Duplicate: Python list append behavior Why does this code: x = [[]]*3 x[0].append('a')
Possible Duplicate: How to convert List<Integer> to int[] in Java? I have an ArrayList
Possible Duplicate: Concatenating a C# List of byte[] I have a list of byte[]
Possible Duplicate: list fragment does not accept my layout I'm trying to use a
Possible Duplicate: Java - Distinct List of Objects i have a sorted array of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.