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 1093405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:49:21+00:00 2026-05-16T23:49:21+00:00

Consider this short python list of dictionaries (first dictionary item is a string, second

  • 0

Consider this short python list of dictionaries (first dictionary item is a string, second item is a Widget object):

raw_results =  
     [{'src': 'tag', 'widget': <Widget: to complete a form today>},   # dupe 1a
      {'src': 'tag', 'widget': <Widget: a newspaper>},                # dupe 2a
      {'src': 'zip', 'widget': <Widget: to complete a form today>},   # dupe 1b
      {'src': 'zip', 'widget': <Widget: the new Jack Johnson album>},
      {'src': 'zip', 'widget': <Widget: a newspaper>},                # dupe 2b
      {'src': 'zip', 'widget': <Widget: premium dog food >}]

I want to go through that list and remove the duplicates, which this SO question answered for me:

Remove duplicates in a list while keeping its order (Python)

    known_widgets= set()
    processed_results = []

    for x in raw_results:
        widget = x['widget']
        if widget in known_widgets: 
            continue
        else:
            processed_results.append(x)
            known_widgets.add(widget)

However, after I remove the duplicate row (e.g. dupe 1b), I want to change the remaining duplicate’s (e.g. dupe 1a) “src” data. I would like to append the removed duplicates “src” to the original. This is what I’d like to end up with:

processed_results =  
    [{'src': 'tag-zip', 'widget': <Widget: to complete a form today>},  # dupe 1a
     {'src': 'tag-zip', 'widget': <Widget: a newspaper>},               # dupe 2a
     {'src': 'zip', 'widget': <Widget: the new Jack Johnson album>},
     {'src': 'zip', 'widget': <Widget: premium dog food >}]

I’m sure this is easy to do, but my head is spinning after too much coffee and many hours circling this problem. I’d love and really appreciate the help of an expert. Thank you!

  • 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-05-16T23:49:22+00:00Added an answer on May 16, 2026 at 11:49 pm
    def find_widget(widget, L):
        for i, v in enumerate(L):
          if v[widget] == widget:
              return i
    
    known_widgets= set()
    processed_results = []
    
    for x in raw_results:
        widget = x['widget']
        if widget in known_widgets:
            processed_widgets[find_widget(widget, processed_results)]['src'] += '-%s' % x['tag']
            continue
        else:
            processed_results.append(x)
            known_widgets.add(widget)
    

    Could probably be done better (as this is two passes for each duplicate widget).

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

Sidebar

Related Questions

Consider this dictionary format. {'KEY1':{'name':'google','date':20100701,'downloads':0}, 'KEY2':{'name':'chrome','date':20071010,'downloads':0}, 'KEY3':{'name':'python','date':20100710,'downloads':100}} I'd like the dictionary sorted by downloads
Consider this code: public class ShortDivision { public static void main(String[] args) { short
consider this simple function def foo(l=[]): if not l: print List is empty else
Consider this simple example - public class Person { private String name; private Date
Consider a Python list my_list containing ['foo', 'foo', 'bar'] . What is the most
Let’s consider this very short snippet of code: #include <stdlib.h> int main() { char*
Possible Duplicate: Is short-circuiting boolean operators mandated in C/C++? And evaluation order? Consider this
Consider this code snippet: public static void main(String[] args) { int z1 = 0;
consider this very short T-SQL code that does a test on a nullable column
Consider this short example: $a = pack(d,255); print length($a).\n; # Prints 8 $aa =

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.