Entry filter for list of directories I'm trying to filter a list composed of multiple dictionaries for useless entries and non-ideal fields. An entry is useless if Mem['visibility'] == "redacted" and then should be removed from the list. An entry is non-ideal when any of the fields are empty, and these should be filled with a 0 instead or if Mem['~whatever~'] is a list in which case the list should be transformed into a single string with all the objects in that list.

class Filter:
    @staticmethod
    def members(memberlist):
        for Mem in memberlist[:]:
            for Item in Mem:
                if not Mem[Item]:
                    Mem[Item] = 0
                if (type(Mem[Item]) is list):
                    Mem[Item] = ', '.join(Mem[Item])
            if(Mem['visibility'] == "redacted"):
                print(Mem)
                memberlist.remove(Mem)
        return(memberlist)


Sample memberlist:

[{'roles': [], 'rank': 'No SCB account', 'type': 'main', 'stars': 2, 'visibility': 'visible', 'sid': 'imperium', 'handle': 'freakyeagle'}, {'roles': [], 'rank': 'Fleet Member', 'type': 'main', 'stars': 1, 'visibility': 'visible', 'sid': 'imperium', 'handle': 'cadimus'}, {'roles': [], 'rank': 'Fleet Member', 'type': 'main', 'stars': 1, 'visibility': 'visible', 'sid': 'imperium', 'handle': 'belleal'}]


How can I make this as simple/efficient as possible?
You could use a list and generator comprehension instead of memberlist[:].
You should use snake_case instead of CamelCase for variables.
You should use isinstance rather than type() is.
This is as it works on objects that extend list. E.g. class MyCustomList(list): pass.
You should remove the print.
They are quite slow, and is probably left over from debugging.


If I were to do the above I would use:

def members(member_list):
    def handle_nonideal(item):
        if not item:
            return 0
        if isinstance(item, list):
            return ', '.join(item)
        return item

    return [
        {
            key: handle_nonideal(item)
            for key, item in member.items()
        }
        for member in member_list
        if member['visibility'] != 'redacted'
    ]