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

Popular posts from this blog

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

matlab - error with cyclic autocorrelation function -

php - Using grpc in Laravel, "Class 'Grpc\ChannelCredentials' not found." -