Remove duplicate values from list of nested dictionaries

Nataly Firstova

I have list of dictionaries with nested structure. I need to remove all duplicate values. I'm newbie in Python and can't solve this task. Anyone can help me?

My list looks like:

[  
   {  
      "task_id":123,
      "results":[  
         {  
            "url":"site.com",
            "date":"04.18.2019"
         },
         {  
            "url":"another_site.com",
            "date":"04.18.2019"
         },
         {  
            "url":"site1.com",
            "date":"04.18.2019"
         }
      ]
   },
   {  
      "task_id":456,
      "results":[  
         {  
            "url":"site3.com",
            "date":"04.18.2019"
         },
         {  
            "url":"site.com",
            "date":"04.18.2019"
         }
      ]
   },
   {  
      "task_id":789,
      "results":[  
         {  
            "url":"site7.com",
            "date":"04.18.2019"
         },
         {  
            "url":"site9.com",
            "date":"04.18.2019"
         },
         {  
            "url":"site.com",
            "date":"04.18.2019"
         }
      ]
   }
]

I need to set site.com only once. If any value of url is duplicated - exclude it from dict.

As result: task 123 with 3 dicts in results task 456 with 1 dict in results (exclude site.com) task 789 with 2 dict in results (exclude site.com)

Desired output should looks like:

[  
   {  
      "task_id":123,
      "results":[  
         {  
            "url":"site.com",
            "date":"04.18.2019"
         },
         {  
            "url":"another_site.com",
            "date":"04.18.2019"
         },
         {  
            "url":"site1.com",
            "date":"04.18.2019"
         }
      ]
   },
   {  
      "task_id":456,
      "results":[  
         {  
            "url":"site3.com",
            "date":"04.18.2019"
         }
      ]
   },
   {  
      "task_id":789,
      "results":[  
         {  
            "url":"site7.com",
            "date":"04.18.2019"
         },
         {  
            "url":"site9.com",
            "date":"04.18.2019"
         }
      ]
   }
]
FindOutIslamNow

let results to be your array.

u = set()
final = []
for dict in results:
   for res in dict["results"]:
      if res["url"] not in u:
         u.add(res["url"])
         final.append(res)
print(final)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Retrieve values from both nested dictionaries within a list

Remove dictionary from list if two dictionaries have duplicate keys

Remove all None values from nested dictionaries

How to extract duplicate keys and values from a list of python dictionaries?

How to remove duplicate from list of values

How to remove dictionaries in a list which have some duplicate values (but not all)

Remove duplicate values from Array of Dictionaries

Python - remove duplicate item from list of dictionaries based on dictionary value

How to remove a character from values in list of dictionaries faster?

Remove duplicates from a list of nested dictionaries

remove duplicate values from a list with multiple properties

How to remove nan values from a dictionaries list of values in python 3?

Remove item from nested dictionaries if specified key contains None values

How to remove duplicate rows from nested list?

Fetching values from dictionaries nested in a list?

How to remove the first duplicate values from a List?

Getting values from a nested list of dictionaries using python

trying to delete duplicate dictionary values from the list of dictionaries

Remove duplicates values from list of dictionaries in python

Find duplicate values in list of dictionaries

remove duplicates by key from nested list of objects of custom dictionaries

Remove string after a predefined string from a nested list of dictionaries with pandas

List of dictionaries, remove dicts with some duplicate key, values

How to remove the duplicate dictionaries from the list of dictionaries where dictionary contains an another dictionary?

Is there a way to insert different values into a list of duplicate dictionaries?

How to get values from list of nested dictionaries?

Making a nested dictionaries from a list of dictionaries

extract values from nested dictionaries in a list of tuples

Remove item from list of dictionaries based on values in another list