Python Forum

Full Version: What is the value after JOINING an empty list?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

I want to set a if condition based on the value of test_final. Sometimes there is no value in test_final if no items are found.

For such cases, I tried to set the if condition based on None or "", but it doesn't work. The code just doesn't enter the if loop, even when test_final is empty. Thank you.

test=[]

some code to insert items to test

test_final=",".join(test)

if test_final=="" or test_final==None:
  print('Hello')
>>> spam = []
>>> eggs = ','.join(spam)
>>> eggs
''
>>> if eggs == "" or eggs == None: # better eggs is None
...     print('Hello')
... 
Hello
your code should work just fine. But there is no need for eggs == None and in any case it's better as eggs in None

Probably there is something in the code not shown here. Check what is the value of test just before join
Or
if not test_final: # Catches None, [], "", 0 and False
  print('Hello')