I want to check dicts in a list which format is :
- It should have key :
class
,subject
,teacher
- And the value of the key can't not be blank
But I got weird result :
In case 1 {'subject':'history','teacher':'Marry'}
is invalid
But the code didn't remove it !
And case 2 works well
Here is what I do :
# case 1
data_list = [{'class':'A','subject':'math','teacher':'Sam'},
{'class':'','subject':'','teacher':''},
{'subject':'history','teacher':'Marry'}]
# case 2
# data_list = [{'teacher': 'Marry', 'subject': 'D'}]
for data in data_list:
message={}
message['class'] = data.get('class',required_field) or blank_field
message['subject'] = data.get('subject',required_field) or blank_field
message['teacher'] = data.get('teacher',required_field) or blank_field
if required_field in message.values() or blank_field in message.values():
print "This dict need to be remove:{}".format(message)
data_list.remove(data)
print "#### final list ####"
print data_list
print "#### final list ####"
case 1 result :
This dict need to be remove:{'teacher': 'This_field_may_not_be_blank', 'class': 'This_field_may_not_be_blank', 'subject': 'This_field_may_not_be_blank'}
#### final list ####
[{'teacher': 'Sam', 'class': 'A', 'subject': 'math'},
{'teacher': 'Marry', 'subject': 'history'}]
#### final list ####
case 2 result :
This dict need to be remove:{'teacher': 'Marry', 'class': 'This_field_is_required', 'subject': 'D'}
#### final list ####
[]
#### final list ####