Possible Duplicate:
How do you check if list is blank?
def CleanWhiteSpace(theDict) :
stuff=[]
for key,value in theDict.items():
for d in value:
if value != " ":
stuff.append(d)
print d
theDict[key]=stuff
if not value[d]:
print value
stuff=[]
return theDict
print CleanWhiteSpace({'a':['1','2'],'b':['3',' '],'c':[]})
how do u check is c is blank is c is simply equal to []?
okay I have a DICTIONARY that has LISTS inside it, some of these lists are composed of NOTHING. I need to loop though the dictionary and remove those keys that have the value of AN EMPTY LIST.
trying to be as clear as possible. what have i tried? i have tried multiple different methods to check if the value of the key is equal to nothing. i tried the value == “”: statement, i tried the, len function to check if the value is empty, i tried if value: nothing seems to work, why? because as someone answered, empty lists don’t get executed in the body of the loop
In your example, when key
cis being processed the value ofkeywill be'c'and the value ofvaluewill be[], an empty list. You can test for this by saying, e.g.,if not value:. Or, if you know it’s a list rather than some other kind of sequence,if value==[].It’s hard to be sure from the question as written, but did you perhaps try putting something like
if d==[]:somewhere inside the loop? That won’t work, because whenvalueis an empty list the loopfor d in value:is looping over the elements of an empty list, and there aren’t any, so the loop body will never be executed.Incidentally, some other bits of your code seem to show confusion between
valueand its elements. For instance, you writeif value != " "but surely it’sdthat you want to be testing there. And what’s withif not value[d]:? Why would you expectdto be a suitable thing to indexvalueby?You should also think a bit more carefully about what happens to
stuffas your code executes.