1

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 Aug 19, 2022 at 2:40
1
  • 4
    What is float('NaN') doing here? Your list only has strings. Commented Aug 19, 2022 at 2:42

5 Answers 5

1

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 Aug 19, 2022 at 3:20
0
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 Aug 19, 2022 at 3:25
0

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 Aug 19, 2022 at 3:02
3
  • No need to use 0 or 1 to represent boolean values, as Python has True and False. Commented Aug 19, 2022 at 3:30
  • I would suggest that you break your loop if both check_1 and check_2 are True. No further checks are necessary if that condition is met. Commented Aug 19, 2022 at 3:32
  • @Chris Yes, this was just a start so he could expand on it. Commented Aug 19, 2022 at 3:54
0

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.

Chris
37.6k6 gold badges33 silver badges58 bronze badges
answered Aug 19, 2022 at 3:14
0

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 Aug 19, 2022 at 3:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.