How would you make a comma-separated string from a list of strings?

后端 未结 14 1245
北海茫月
北海茫月 2020-11-22 16:07

What would be your preferred way to concatenate strings from a sequence such that between every two consecutive pairs a comma is added. That is, how do you map, for instance

14条回答
  •  醉话见心
    2020-11-22 16:30

    >>> my_list = ['A', '', '', 'D', 'E',]
    >>> ",".join([str(i) for i in my_list if i])
    'A,D,E'
    

    my_list may contain any type of variables. This avoid the result 'A,,,D,E'.

提交回复
热议问题