-1

I would like to create a variable named "allvar" that has all values in mylist for example :

mylist = ['X', 'Y', 'Z']

How can I create allvar to be something like .. X Y Z seperated by one space instead of "," and without [] and with out "single quote"

allvar = X Y Z

The reason I like to get allvar because I need to pass this allvar to another host server that have this format already in place ..

Any recommendation ?

JPC
  • 5,063
  • 20
  • 71
  • 100
  • without single quotes makes it a variable, that too a invalid one. – Ashwini Chaudhary Oct 10 '12 at 22:41
  • Why do you want to do this? ...you can't since this X Y Z isn't a valid datatype. – Andy Hayden Oct 10 '12 at 22:42
  • I said I need to pass this value to another server, because list can't be used to pass to another server. X Y Z meaning String , so I'm actually looking for something like 'X Y Z' – JPC Oct 10 '12 at 23:05
  • Sorry, I should have been more cleared. – JPC Oct 10 '12 at 23:05
  • This is probably a duplicate of ["how to join list of strings."](http://stackoverflow.com/questions/2426988/how-to-join-list-of-strings) – dbn Oct 10 '12 at 23:26

2 Answers2

5

You need to use join:

>>> mylist = ['X', 'Y', 'Z']
>>> allvar = ' '.join(mylist)
>>> print allvar
X Y Z
Iliyan Bobev
  • 3,070
  • 2
  • 20
  • 24
1

It's not exactly clear what you are trying to do here. If you are just trying to send the string 'X Y Z' with X Y and Z being replaced with floating point variables then this will do it:

allvar = '%f %f %f' % (X,Y,Z)

String formatting types (eg '%f') for float can be found here: http://docs.python.org/library/stdtypes.html#string-formatting

If you are working with more complex types than floats, ints, strings, etc or if you need a more compact representation of the string then you could use a serialization protocol like msgpack, json, etc. However, the remote server would have to be able to unpack the data that's been packed using a serializer.

dhj
  • 512
  • 5
  • 16