def simple_filter(
    data=None,
    attr=None,
    value=None,
    property_field=None,
):

    # Take the do-nothing case shortcut
    if not data or not attr or value is None:
        return data

    # NOTE:(dtroyer): This filter modifies the provided list in-place using
    # list.remove() so we need to start at the end so the loop pointer does
    # not skip any items after a deletion.
    for d in reversed(data):
        if attr in d:
            # Searching data fields
            search_value = d[attr]
        elif (property_field and property_field in d and
                isinstance(d[property_field], dict)):
            # Searching a properties field - do this separately because
            # we don't want to fail over to checking the fields if a
            # property name is given.
            if attr in d[property_field]:
                search_value = d[property_field][attr]
            else:
                search_value = None
        else:
            search_value = None

        # could do regex here someday...
        if not search_value or search_value != value:
            # remove from list
            try:
                data.remove(d)
            except ValueError:
                # it's already gone!
                pass

    return data
Filter a list of dicts
dataset/ETHPy150Open dtroyer/python-openstackclient/openstackclient/api/utils.py/simple_filter