How to turn a list of int's to a string with a ',' seperator

This is the place for queries that don't fit in any of the other categories.

How to turn a list of int's to a string with a ',' seperator

Postby Pharo17 » Thu May 22, 2014 11:46 am

I am a newbe at python programming, programming in general.

I have a function with a list:
Code: Select all
alist=[2,4,5,7,8,2,1,4,,7,8,1]

I wan't it to print out like this:
Code: Select all
 2,4,5,7,8,2,1,4,,7,8,1


my code
Code: Select all
 def myString(alist):
s=''
for i in range(len(alist)):
    s=s+str(i)+','
return s


I can get it the function print the list with the comma on the end, but i do not wan't the last comma to print
How can i achieve this.
Thank you
Last edited by Yoriz on Thu May 22, 2014 12:06 pm, edited 1 time in total.
Reason: Changed title.
Pharo17
 
Posts: 3
Joined: Wed Apr 09, 2014 3:49 am

Re: lists

Postby Yoriz » Thu May 22, 2014 12:01 pm

Code: Select all
alist=[2,4,5,7,8,2,1,4,,7,8,1]

Your list has no value between 4 and 7 and would produce a syntax error.

Code: Select all
alist = [2, 4, 5, 7, 8, 2, 1, 4, 0, 7, 8, 1]

print ','.join(map(str, alist))

Output
Code: Select all
2,4,5,7,8,2,1,4,0,7,8,1
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: buran and 4 guests