-1

Input : [12,13,14,15]

O/p : 12,13,14,15

O/p should be int

Any suggestions on how to i/p a list of integers and get the o/p as integers but without the square braces?

DYZ
  • 55,249
  • 10
  • 64
  • 93
Sana
  • 55
  • 4
  • 2
    Output should a string? Can you show what you have tried already? – Rahul Bharadwaj May 03 '20 at 05:48
  • 1
    Is the input a string with brackets or is it an actual Python list? Is your output a string or a series of integers (e.g. a Python list, tuple, numpy array)? Showing some code would also clarify that. – roadrunner66 May 03 '20 at 06:09
  • 1
    There is no such thing as "comma-separated integers." Do you expect a string of integers separated by commas? – DYZ May 03 '20 at 06:29

3 Answers3

1

The simplest way to this is to use "*" in print and using separator as ",".

The default separator is " " (Space).

lst = [12,13,14,15]
print(*lst,sep=",")
SajanGohil
  • 960
  • 13
  • 26
Anurag Singh
  • 126
  • 5
0

if you want in one string

l=[12,13,14,15]
print(','.join(l))

OR

l=[12,13,14,15]
for i in l[:-1]:
    print(i,end=',')
print(l[-1])

However while printing data type does not matter

-1

Your question is not really clear about the output, but if you want a string as output, you can do like this :

lst = [12,13,14,15]
str1 = ''.join(str(e)+',' for e in lst)
l = len(str1)-1
str1 = str1[:l]

then you can do what you want with str1 (print it ect...)

kiliz
  • 95
  • 4
  • 1
    or `','.join(str(e) for e in lst)`, that is the purpose of join – SajanGohil May 03 '20 at 06:14
  • @SajanGohil WARNING : your way to do works, but you have to remove `l = len(str1)-1` and `str1 = str1[:l]` or the last digit will disappear ! – kiliz May 03 '20 at 06:16
  • that's the whole point, if you use join that way, you don't need extra processing – SajanGohil May 03 '20 at 06:18
  • yup, but I prefer to mention it (just in case somebody else read too fast ^^) – kiliz May 03 '20 at 06:20
  • You should not answer a question that is not clear to you. – DYZ May 03 '20 at 06:30
  • I disagree with you @DYZ , I prefer to be wrong because I misunderstood and ask precisions than let somebody without any help because he has a hard time being understood. I don't pretend to have all answers, but if I can give a good idea and help, I do my best. (And if my answer is not suitable it will be ignored and will eventually be relegated to the bottom of the thread). – kiliz May 03 '20 at 06:42