Python convert list of one element into string with bracket -
how convert python list containing 1 element string bracket? more 1 element, easy me just use tuple(list['a','b']) returns tuple ('a','b') if element one, returns ('a',) rather want return ('a') sample:
mylist = ["a", " b"] print tuple([s.strip() s in mylist]) >> ('a', 'b')  mylist = ["a"] print tuple([s.strip() s in mylist]) >> ('a', ) #rather want return ('a') 
avoid relying on default __repr__() method, format strings, might change.
be explicit intent instead
 print "('" + "', '".join(mylist) + "')" 
Comments
Post a Comment