1

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

Community
  • 1
  • 1
Web Master
  • 4,240
  • 6
  • 20
  • 28
  • Didn't we just have this question, or is this different? – Levon Jun 14 '12 at 00:04
  • We solved the last one, this is the last question, but it has a new problem – Web Master Jun 14 '12 at 00:05
  • @ChinmayKanchi nope, i got rid of that question, but some dude named sven is trying to roll it back, just to make this a possible duplicate. I asked one question, then when it was solved, edited it to something else. then edited again and created a new question. someone is constantly rolling both questions to a point where they were the same just to get me in trouble. original copies of the questions are both different. – Web Master Jun 14 '12 at 00:12
  • 1
    Your question would be easier to answer (and would be getting fewer votes to close it) if it made clear (1) what you mean by, e.g., "I tried `==[]`", (2) exactly what happened when you tried that, and (3) what you wanted to happen that was different. (What is your `CleanWhiteSpace` function actually supposed to do?) – Gareth McCaughan Jun 14 '12 at 00:20
  • I think it is hugely unlikely that anyone is doing anything "just to get you in trouble" (why on earth would they?). What actually seems to have happened is that your question got closed and you tried to delete its text with an angry message. Don't do that. It's not productive. – Gareth McCaughan Jun 14 '12 at 00:23
  • Also, insulting the mods is a bad way to get help. Most (all?) of the mods on SO have earned the right by patiently answering questions and making helpful edits for a substantial period of time, and have therefore built up a lot of goodwill. Insulting them is simply going to make it less likely that the community will help you :). Be nice. – Chinmay Kanchi Jun 14 '12 at 00:29
  • like i explained, instead of making an another question, i tried to edit a solved question. then when no one was answering, i tried to make a new question. so my edit and a new question were the same. i then deleted the edited question. then someone called sven rolled back the first question, not to the original original question, but to the exact point where questions were duplicates. why didn't he roll it back to original state, but the duplicate state? just to be ill productive with ill wishes. after he did it 3 times i wrote the angry message – Web Master Jun 14 '12 at 00:30
  • @ChinmayKanchi so why was this mod constantly mutilating my questions to look like duplicate's? he could have rolled the first question all the way to the begging. – Web Master Jun 14 '12 at 00:31
  • @GarethMcCaughan i edited my question to your specifications – Web Master Jun 14 '12 at 00:34
  • Finally! Now that we know what you want to do, I've given you a solution. – Chinmay Kanchi Jun 14 '12 at 00:40
  • It looks to me as if he simply undid your change that attempted to delete the question. If he'd noticed that you'd earlier edited an already-answered question to make it ask something different (!!!) then he might well have undone that change too, but perhaps he didn't notice. Seriously: No one is attempting to harm you here, and if you can't believe that then I'm afraid the problem is yours. – Gareth McCaughan Jun 14 '12 at 00:42
  • Please, name your functions not like MyFunction, but like my_function (PEP8) – Aleksei astynax Pirogov Jun 14 '12 at 02:43

3 Answers3

1

In your example, when key c is being processed the value of key will be 'c' and the value of value will 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 when value is an empty list the loop for 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 value and its elements. For instance, you write if value != " " but surely it's d that you want to be testing there. And what's with if not value[d]:? Why would you expect d to be a suitable thing to index value by?

You should also think a bit more carefully about what happens to stuff as your code executes.

Gareth McCaughan
  • 19,888
  • 1
  • 41
  • 62
  • sigh. thank you for your explanation of why it wasn't working. so how do you make it work? how do u remove an empty list from a dictionary from an array? – Web Master Jun 14 '12 at 00:20
  • I am not going to write your code for you, and most likely neither is anyone else; that isn't how Stack Overflow works. (Aside from anything else, for all we know this is some kind of homework assignment and solving it for you would make it harder for you to learn.) You need to be more specific about what it is you want to know. – Gareth McCaughan Jun 14 '12 at 00:29
  • 1
    Incidentally, note that both I and Chinmay *did* say how to do it right: the usual way to check whether a list is empty is to say something like `if not value:`. If for some reason this doesn't work for you, *show us how it fails* and then maybe we can help further. – Gareth McCaughan Jun 14 '12 at 00:33
  • I agree with Gareth. "they don't work in this specific snippet that i have" is not helpful. We can't fix code that isn't there. – Chinmay Kanchi Jun 14 '12 at 00:34
  • What part is confusing? gareth said, since the variable value is empty, the emptiness doesn't get pulled through the main loop. How do you delete empty lists from the dictionary? what do you guys need me to explain? {'a': ['1', '2'], 'c': [], 'b': ['3', ' ']} here is how it fails. key c has the value of "[]". key c is not supposed to exist. and the code is already written. it just doesn't work – Web Master Jun 14 '12 at 00:36
  • found solution, ty for trying +1 – Web Master Jun 14 '12 at 00:46
1

EDIT:

How about this?

def CleanWhiteSpace(theDict):
    newDict = {}
    for key in theDict:
        if theDict[key]:
            newDict[key] = theDict[key]
    return newDict

Note that this avoids modifying theDict in place, so if you want that, just ask.

There are two ways to check if a list is blank:

a_list = []
if not a_list:
    do something #the if is entered if the list is blank

Or

a_list = []
if len(a_list)==0:
    do something

The two snippets are equivalent. Note that the first one works because the value of a_list is implicitly converted to a boolean and bool([]) is False.

Chinmay Kanchi
  • 62,729
  • 22
  • 87
  • 114
0
theDict = dict( [(k,v) for k,v in theDict.items() if len(v)>0])
Web Master
  • 4,240
  • 6
  • 20
  • 28