469,898 Members | 1,768 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

items in an array

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

Apr 19 '06 #1
6 1530
Ant
If you just want the items concatenated with a comma separator, the
following is what you need:
list_arr = ["one", "two", "three"]
list = ",".join(list_arr)
print(list)

one,two,three

Apr 19 '06 #2
> 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

Apr 19 '06 #3
>>> list_array = ['aaa','bbb','ccc']
for item in list_array:

.... 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?

--
Renato Ramonda

Apr 19 '06 #4
> 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)

Apr 19 '06 #5
Hi All -

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

Regards -
Shawn

Apr 19 '06 #6
sh******@gmail.com <sh******@gmail.com> wrote:
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".)

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Apr 19 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Colin Steadman | last post: by
reply views Thread by tommazzo | last post: by
4 posts views Thread by mb | last post: by
10 posts views Thread by pamelafluente | last post: by
reply views Thread by kasthurirangan.balaji | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.