J
J
Juster2011-02-25 10:22:54
Python
Juster, 2011-02-25 10:22:54

How to concisely get a string from a list of ints

There is a list of ints, you need to get a string like "1,3,5,12", but so that the code is concise (in one line).
a = [1, 3, 5, 12]
str_ =…

Answer the question

In order to leave comments, you need to log in

6 answer(s)
V
Vlad Frolov, 2011-02-25
@Juster

reduce(lambda x, y: '%s,%d' % (x, y), a)

S
SeTeM, 2011-02-25
@SeTeM

str(a)[1:-1]

B
bekbulatov, 2011-02-25
@bekbulatov

', '.join(map(str, a))

Z
zholdas, 2011-02-25
@zholdas

It is possible like this:",".join("%s" % str(item) for item in a)

J
Juster, 2011-02-25
@Juster

reverse version, from string to list: [int(s) for s in str_.split(',')]

A
Alexey Shein, 2011-02-25
@conf

Apparently, all pythonists are here, I will write for js :)
> [1, 3, 5, 12].join()
"1,3,5,12"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question