Python Forum
What is the value after JOINING an empty list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the value after JOINING an empty list?
#1
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')
Reply
#2
>>> 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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Or
if not test_final: # Catches None, [], "", 0 and False
  print('Hello')
buran and DeaD_EyE like this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code with empty list not executing adeana 9 3,725 Dec-11-2023, 08:27 AM
Last Post: buran
  Joining two jupyter notebooks and getting an error! Led_Zeppelin 1 1,129 Oct-20-2022, 04:28 PM
Last Post: deanhystad
  set.difference of two list gives empty result wardancer84 4 1,474 Jun-14-2022, 01:36 PM
Last Post: wardancer84
  displaying empty list vlearner 5 1,648 Jan-19-2022, 09:12 AM
Last Post: perfringo
  Remove empty keys in a python list python_student 7 3,012 Jan-12-2022, 10:23 PM
Last Post: python_student
  Printing empty list? hhydration 2 2,109 Oct-28-2020, 11:34 AM
Last Post: Atekka
  Stop a function if the list it needs is empty Pedroski55 2 2,916 Jul-25-2020, 11:50 PM
Last Post: Pedroski55
  Need help to make an empty list with possibility to add Arnsol 1 1,801 Mar-19-2020, 06:08 PM
Last Post: michael1789
  append list to empty array SchroedingersLion 1 2,167 Feb-02-2020, 05:29 PM
Last Post: SchroedingersLion
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 2,292 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020