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

12
  • Didn't we just have this question, or is this different? Commented Jun 14, 2012 at 0:04
  • We solved the last one, this is the last question, but it has a new problem Commented Jun 14, 2012 at 0: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. Commented Jun 14, 2012 at 0: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?) Commented Jun 14, 2012 at 0: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. Commented Jun 14, 2012 at 0:23

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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?
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.
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.
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.
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
|
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.

4 Comments

they don't work in this specific snippet that i have, i don't know why
Where in the code are you testing for the empty list? Show us that code.
its all there. the code is being executed form print statement print CleanWhiteSpace({'a':['1','2'],'b':['3',' '],'c':[]})
okay.. how does this delete empty spaces? and its all good i found a solution +1 anyway
0
theDict = dict( [(k,v) for k,v in theDict.items() if len(v)>0]) 

1 Comment

Change that to theDict = dict( [(k,v) for k,v in theDict.items() if v). It's more Pythonic.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.