Imagine I have some coordinates given as a list of tuples:
coordinates = [('0','0','0'),('1','1','1')]
and I need it to be a list as follows:
['XYZ', ['CO', 'X', '0', 'Y', '0', 'Z', '0'], ['CO', 'X', '1', 'Y', '1', 'Z', '1'],'ABC']
But I don't know in advance how many tuples there are in coordinates
and I need to create the list dynamically.
First I used a loop to create the list without 'XYZ'
:
pointArray = []
for ii in range(0, len(coordinates)):
pointArray.append(
[
"CO",
"X" , coordinates[ii][0],
"Y" , coordinates[ii][1],
"Z" , coordinates[ii][2]
])
Then I appended 'XYZ'
to the front and 'ABC'
to the end:
output = pointArray
output [0:0] = ["XYZ"]
output.append("ABC")
Which gives me the desired output. But please consider this just as an example.
I'm not looking for alternative ways to append, extend, zip or chain arrays.
What I actually want to know: is there any syntax to create the list output
also the following way:
output = ["XYZ", pointArray[0], pointArray[1], "ABC"]
but dynamically? So basically I'm looking for something like
output = ["XYZ", *pointArray, "ABC"]
which just seems to work for function arguments like
print(*pointArray)
To sum up: How can I transform a list to a sequence of its comma-separated elements? Is that even possible?
PS: In Matlab I was just used to use the colon {:}
on cell arrays, to achieve exactly that.
Background
I'm recording Python skripts with an external application including the above mentioned lists. The recorded scripts sometimes contain over a hundred lines of codes and I need to shorten them. The easiest would be to just replace the looooong lists with a predefined-loop-created list and extend that array with the desired syntax.