Python Forum
remove duplicates from dicts with list values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
remove duplicates from dicts with list values
#11
(May-24-2024, 04:10 PM)deanhystad Wrote: I'm not sure if this is right since you never clarified what "duplicate" means in this particular case, but here's your band-aid.
dict1 = {"SAG01112_SSAP_HA_LPM": [["OS_TYPE", "AIX"], ["IS_COBOL", "1"]], "SAP": [], "C11_RG": [], "W11_RG": []}
dict2 = {
    "SAG01112_SSAP_HA_LPM": [
        ["OS_TYPE", "AIX"],
        ["IP", "172.17.10.112"],
        ["IP", "10.111.160.119"],
        ["IP", "10.111.160.68"],
        ["IP", "10.111.160.66"],
        ["IP", "10.95.0.112"],
        ["IP", "10.111.162.119"],
    ],
    "SAP": [],
    "C11_RG": [],
    "W11_RG": [],
}


def remove_common_items(dict_a, dict_b):
    """Remove values that are common to a and b."""
    # For common keys
    for key in set(dict_a) & set(dict_b):
        a = dict_a[key]
        b = dict_b[key]
        # Remove items common to both value lists.
        for item in [x for x in a if x in b]:  # Make list of common before iterating
            a.remove(item)
            b.remove(item)


remove_common_items(dict1, dict2)
print(dict1)
print(dict2)
I still think you dictionaries are messed up and should look like this:

hm...looks also usable..but not sure if sets will work with embedded lists as lists are not hashable afaik. will try...
and no need to nitpick on the term "duplicate"...if a value is in both dicts i call this duplicate, and i want to remove it in the second dict, easy as that.
Reply


Messages In This Thread
RE: remove duplicates from dicts with list values - by wardancer84 - May-24-2024, 04:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 670 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Copying the order of another list with identical values gohanhango 7 1,429 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,462 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Comparing List values to get indexes Edward_ 7 1,481 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 3,071 Jan-24-2023, 08:22 AM
Last Post: perfringo
  user input values into list of lists tauros73 3 1,260 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  remove partial duplicates from csv ledgreve 0 932 Dec-12-2022, 04:21 PM
Last Post: ledgreve
  Remove values for weekend in a panda series JaneTan 0 775 Dec-12-2022, 01:50 AM
Last Post: JaneTan
  Remove numbers from a list menator01 4 1,623 Nov-13-2022, 01:27 AM
Last Post: menator01
  Remove if similar values available based on two columns klllmmm 1 1,494 Feb-20-2022, 06:55 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020