[Python] Simple pythonic way to check if a list of dict is a subset of another list of dict?

Discussão em 'Python' iniciado por Stack, Abril 23, 2025.

  1. Stack

    Stack Membro Participativo

    Is there a simple pythonic way to check if a list of dictionaries is a subset of another list of dictionaries? This can be done via a for loop by checking each item. I'm hoping there's a faster way with built-in methods.

    i.e.

    maybe_subset = [
    {'Key': 'apple', 'Value': '1234'},
    {'Key': 'orange', 'Value': '2431'},
    {'Key': 'banana', 'Value': '9999'}
    ]

    maybe_superset = [
    {'Key': 'orange', 'Value': '2431'},
    {'Key': 'banana', 'Value': '9999'},
    {'Key': 'creator', 'Value': 'JOHNSMITH'},
    {'Key': 'apple', 'Value': '1234'}
    ]

    def subset(a,b):

    something


    so running subset(maybe_subset,maybe_superset) should return true

    issubset doesn't work since it's a list of dict which is unhashable.

    Continue reading...

Compartilhe esta Página