The semantics of turning a list into a string

Many programming languages, including Python, support this operation:

", ".join(["1","2","3"])

which returns a string

"1, 2, 3"

I understand that this is so, but I do not understand the solution behind it - it would probably be more semantically correct to perform an operation joinon the list, for example:

["1","2","3"].join(", ")

If anyone could explain the design decision and shed light on it, I would appreciate it.

Edit: It looks like Javascript has a method joinon the list; if anyone has examples followed by certain languages, feel free to comment / respond to a choice in that particular language.

+5
source share
1 answer

python . -, , , join , - . join .. ..

:

", ".join(["1","2","3"])
", ".join(("1","2","3"))
", ".join(str(x) for x in xrange(1,4))
", ".join({'1':None,'2':None,'3':None})
", ".join({'1','2','3'})  #set literal syntax -- python2.7+
", ".join("123")

6 , ( ).

-, join , basestring. , , . , API ( ). , , list.remove , . . . , join. , , , , , , 1.

+3

All Articles