I have a filter method that receives a list composed of dictionaries. You must discard dictionaries that have their handle
or their citizen_number
elements off the filtered result as well as concatenate any lists inside that dictionary. In addition to that, in case an element on that main list is a None
it should be skipped and all the None
dictionary elements should be replaced by a zero. This is the solution that I have come up with to do so
def citizens(citizen_list):
filtered_list = []
try:
for citizen in citizen_list:
if citizen:
for item in citizen:
if not item:
citizen[item] = 0
if (citizen['handle'] == 0 or citizen['citizen_number'] == 0):
continue
if (citizen['organizations'] is not None):
citizen['organizations'] = ', '.join(
org['sid'] for org in citizen['organizations'])
else:
citizen['organizations'] = 0
for item in citizen:
if(type(citizen[item]) is list):
citizen[item] = ', '.join(citizen[item])
filtered_list.append(citizen)
return(filtered_list)
except Exception:
print(citizen)
raise
Note that the list concatenation for the organizations
field must be done before the general one and in that particular way. I feel this code just looks awfully hacked together, how can I make this better?