0

I have a list as follows:

sample = ['a', 'b', 'c', None]

My Code is :

sample_out = ','.join(sample)

Error:

TypeError: sequence item 3: expected string, NoneType found

Could someone point me out how to handle the None?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Does this answer your question? [Join a list of items with different types as string in Python](https://stackoverflow.com/questions/3590165/join-a-list-of-items-with-different-types-as-string-in-python) – Tomerikoo Jun 06 '20 at 13:27
  • You didn't specify how you actually **want** to *handle* the `None`... Do you want to add it to the string? Filter it out? print a warning? – Tomerikoo Jun 06 '20 at 13:31
  • if you could provide the expected output that would make things easier – Anurag Wagh Jun 06 '20 at 13:32
  • I would filter out the None and result would be like a,b,c – Arun Sunderraj Jun 06 '20 at 13:33
  • In that case, here is the correct dup: https://stackoverflow.com/questions/14229433/native-python-function-to-remove-nonetype-elements-from-list – Tomerikoo Jun 06 '20 at 13:38

1 Answers1

0

Try this,

sample_out = ','.join([x if x else "" for x in sample])
sushanth
  • 8,275
  • 3
  • 17
  • 28