items in an array

Tim Chase python.list at tim.thechases.com
Wed Apr 19 10:14:06 EDT 2006


>   list_array = []
>   list = item1,item2,itemN...

My first recommendation would be that you not use "list" as 
an identifier, as it's a builtin function.  Odd bugs might 
start happening if you redefine it.

> I can get list to be how I want it if I use the index value as follows:
> 
>   list = ("%s" + "," + "%s", ...) % (list_array[0], list_array[1], ...

If I understand correctly what you want, you're looking to 
create a string that consists of commas separating each 
element of your array.  In such case, what you want is

result = ",".join(list_array)

or if you want spaces after your commas, the boringly 
trivial modification:

result = ", ".join(list_array)

If instead you want the result as a tuple, you can just use 
the tuple() function:

	tuple_result = tuple(list_array)

If you want a tuple containing just the one string (which it 
strangely seems like your example is doing), you can do

	one_string_tuple = (",".join(list_array),)

(note the peculiar "trailing comma in parens creates a 
one-element tuple" syntax...it often catches new Python 
programmers off-guard)

HTH,

-tim








More information about the Python-list mailing list