0

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 ####
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
user2492364
  • 6,543
  • 22
  • 77
  • 147

2 Answers2

2

AJK has pointed out what's going wrong with your code, but you can also make it much shorter, and therefore easier to understand. Bear in mind that the dictionary get method will return None if the given key isn't in the dict, and that both None and the empty string "" evaluate to false.

So the easiest way to proceed would be to create a new list of the acceptable entries as follows:

result = []
for d in data_list:
    if d.get("class") and d.get("subject") and d.get("teacher"):
        result.append(d)

You can shorten this by using a comprehension:

result = [d for d in data_list
          if d.get("class") and d.get("subject") and d.get("teacher")]
holdenweb
  • 33,305
  • 7
  • 57
  • 77
0

This is because you should never delete items from a list which you're iterating over using a for.

Here is a similar module : problem Deleting list items in a for loop (python)

use th copy module. copied_list = copy.deepcopy(original_list)

Community
  • 1
  • 1
AbdealiLoKo
  • 3,261
  • 2
  • 20
  • 36