items in an array

S

shkelley

Hi All -

I am working on a project in which I generate an array of values
(list_array). I need to use the values in this array to create list
similar to the one below:

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

I am having difficulty in getting the values out of the original array.
I have tried enumerating over the array, but the results are not what
I need. When I attempt to simply print list, I get the following
output:

print list
['item1', 'item2', ..., 'itemN']

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], ...

However, the list_array will never contain a constant number of items.
So my dilema is how to loop/iterate through list_array to create list
in the format I want.

Any suggestions are greatly appreciated.

-Shawn
 
T

Tim Chase

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
 
R

Renato

list_array = ['aaa','bbb','ccc'].... print item + ',',
....
aaa, bbb, ccc,


(notice the comma at the end of the print statement: this causes the
suppression of the automatic newline)

Is this what you need?
 
D

Daniel Nogradi

I am working on a project in which I generate an array of values
(list_array). I need to use the values in this array to create list
similar to the one below:

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

I am having difficulty in getting the values out of the original array.
I have tried enumerating over the array, but the results are not what
I need. When I attempt to simply print list, I get the following
output:

print list
['item1', 'item2', ..., 'itemN']

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], ...

However, the list_array will never contain a constant number of items.
So my dilema is how to loop/iterate through list_array to create list
in the format I want.

Any suggestions are greatly appreciated.

I'm not sure if I understand exactly what you want, but if all you
need is turning a list into a tuple then just use the function tuple:
mylist = [ 1, 2, 3 ]
mytuple = tuple( mylist )
print mylist [1, 2, 3]
print mytuple (1, 2, 3)
 
S

Shawn Kelley

Hi All -

Thanks to everyone for their input. The repsonses provided are exactly
what I was looking for!

Regards -
Shawn
 
S

Sion Arrowsmith

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], ...

However, the list_array will never contain a constant number of items.
So my dilema is how to loop/iterate through list_array to create list
in the format I want.

I think what you want is:

list_string = ",".join(list_array)

(Don't use the name "list" as it shadows the builtin "list".)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,508
Members
48,193
Latest member
DannyRober

Latest Threads

Top