0

I have the following output but I want to eliminate the empty lists. How can I do this? It seems that the single quote within the list make it seems like there is something in the list.

[{'Segment': {'Price': 305, 'Mw': 13, '@Number': '1'}}]
[{'Segment': {'Price': 258.43, 'Mw': 46.9, '@Number': '1'}}] 
['']
['']
['']

I tried using the code below but it did not work.

if not a:   
    print "List is empty"
augurar
  • 12,081
  • 6
  • 50
  • 65
Alex
  • 75
  • 1
  • 9
  • What are the possible list contents? Does each list only contain one item? Are there any other "empty" values you want to eliminate, or just `['']`? – augurar May 20 '15 at 04:15
  • I just need to eliminate [''] Thanks – Alex May 20 '15 at 04:18

7 Answers7

2

What you had was a list with a single entry with empty string. It's not an empty list. Best way to check if a list is empty is a correct way to check for empty list.

If you want to check for [''] just do

if a == ['']:
Community
  • 1
  • 1
Nat
  • 3,587
  • 20
  • 22
2

Your list is not empty, it has one empty string on it. You can use ''.join and check as answered:

if not ''.join(a):
   do your thing

I guess you can use any as well on this if your list is sure to have just empty strings.

if any(a):
    do your thing
sagarchalise
  • 992
  • 8
  • 14
  • I was thinking that, but `any()` also returns True on a truly empty list. – gojomo May 20 '15 at 04:17
  • @gojomo Do you mean `any([])` will be True? I think `all([])` is True and not `any`.The problem I see with any is if the list allows other values as [0] etc where `any` is wrong. – sagarchalise May 20 '15 at 04:20
  • Yep, never mind, I was still thinking in mode of question, trying to trigger on the 'empty-or-filled-with-empties' condition, and had just tested `all()`. Using `any()` to trigger positive action or `not any()` to trigger the nothing-there handling (like discarding the list) should work. – gojomo May 20 '15 at 04:24
0
if ''.join(out[0]) != "":
    return out
farhawa
  • 10,120
  • 16
  • 49
  • 91
0

Python counts the empty string as a string. Use a regex if you need to, or merely:

if list[0] != '':
    print(list)

Plug that conditional into a For loop as necessary.

manglano
  • 844
  • 1
  • 7
  • 21
0

Is this what you want?

old_list=[[{'Segment': {'Price': 305, 'Mw': 13, '@Number': '1'}}], 
   [{'Segment': {'Price': 258.43, 'Mw': 46.9, '@Number': '1'}}], [''], ['']]

new_list = []

for i in old_list:
    if i != ['']:
        new_list.append(i)

print new_list
[[{'Segment': {'Price': 305, '@Number': '1', 'Mw': 13}}], [{'Segment': {'Price': 258.43, '@Number': '1', 'Mw': 46.9}}]]
Plug4
  • 3,838
  • 9
  • 51
  • 79
0

Can i try this way:

>>> def is_empty(ls):
...     if all([not len(ls), not ls]):
...         print "List is empty"
...     else:
...         print "List is not empty"
...
>>> is_empty([])
List is empty
>>> is_empty([""])
List is not empty
>>> is_empty([{'Segment': {'Price': 305, 'Mw': 13, '@Number': '1'}}])
List is not empty
>>>
James Sapam
  • 16,036
  • 12
  • 50
  • 73
0

Your list is not empty, it contains an empty string.

If you want to check that your list contains any items that are non-empty you can either use any:

list = [ '' ]
if any(list):
    # List contains non-empty values

or you can filter it before you use it to remove any empty strings:

list = [ '' ]
list = filter(None, list) # Remove empty values from list
if list:
    # List contains items
Raniz
  • 10,882
  • 1
  • 32
  • 64