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

后端 未结 14 1188
北海茫月
北海茫月 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:38

    @jmanning2k using a list comprehension has the downside of creating a new temporary list. The better solution would be using itertools.imap which returns an iterator

    from itertools import imap
    l = [1, "foo", 4 ,"bar"]
    ",".join(imap(str, l))
    

提交回复
热议问题