How to check if a list has both empty and non-empty values in python

Question:

I am trying to check if a list has both empty and non-empty values. My code is as follows.

list = ['','a','b','']
not_empth = not float('NaN')

if float('NaN') and not_empth in list:
    print('List has both empty and non-empty values.')
else:
    print('List does not have both empty and non-empty values.')

However, the output shows as below, which is not correct.

List does not have both empty and non-empty values.
Asked By: CanaryTheBird

||

Answers:

You could use a for loop, as a different approach to this. Please note I changed float("NaN") to '' due to the reason @j1-lee said.

PLEASE NOTE: This approach is not recommended if you have a large list.

list = ['','a','b','']

check_1 = 0 # 0 being represented as false.
check_2 = 0 

for item in list:
    if item == '':
        check_1 = 1
    else:
        check_2 = 1

if check_1 and check_2 == 1:
    print(True)
else:
    print(False)

Output: True

Answered By: Brody Critchlow

What you have in the question just needs an if in operation to solve.

>>> list_ = ['','a','b','']
>>> if '' in list_:
...     print('List has both empty and non-empty values.')
... elif list_ == []:
...     print('List does not have both empty and non-empty values.')
...
List has both empty and non-empty values.

As long you have at least one '', the first condition will be fulfilled. For your second print statement, if I understand correctly, it is to check an empty list.

This checks to see if a list contains both empty strings and non-empty strings

list = ['','a','b','']

has_empty = any(s == '' for s in list) #for items in list, if any equal '' then return true
has_non_empty = any(s != '' for s in list) #for items in list, if any do not equal '' then return true

if has_empty and has_non_empty:
    print('List has both empty and non-empty values.')
else:
    print('List does not have both empty and non-empty values.')
Answered By: Josh Ackland
list = ["Efsf","efsfs",""]
list1 = ["",'']
list2 =  ["fsef",5 ,5]

def check(list):
    null =''
    if null in list and len(list)-list.count(null)>0:
        print("List has both empty and non-empty values.")
    else:
        print("List does not have both empty and non-empty values.")
check(list)
check(list1)
check(list2)

output:

List has both empty and non-empty values.

List does not have both empty and non-empty values.

List does not have both empty and non-empty values.

Answered By: andy wong

Having some fun with functools.reduce to accomplish this in one pass over the list.

lst = ['', 'a', 'b', '']

reduce(lambda acc, x: (acc[0], True) if x != '' else (True, acc[1]), 
       lst, (False, False))
# (True, True)

all(reduce(lambda acc, x: (acc[0], True) if x != '' else (True, acc[1]), 
           lst, (False, False)))
# True

Of course, this does have to evaluate every element in the list and cannot short-circuit. To do that, we might implement a function reduce_until which lets us supply a function that provides a condition for early exit.

def reduce_until(f, cond, iter, init):
    for i in iter:
        if cond(init): return init
        init = f(init, i)
    return init

reduce_until(lambda acc, x: (acc[0], True) if x != '' else (True, acc[1]),
             all, lst, (False, False))
# (True, True)

all(reduce_until(lambda acc, x: (acc[0], True) if x != '' else (True, acc[1]),
                 all, lst, (False, False))
# True
Answered By: Chris
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.