-2
list_a=[362,673,196,452,972]

I want to convert this list_a into a string

Individually for each index of the list_a I'm able to convert to string. For ex: list_a[0]=str(362) but i want to convert the list at once.

001
  • 13,291
  • 5
  • 35
  • 66
user11582315
  • 21
  • 1
  • 5
  • [List comprehension](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions) is one way: `new_list = [str(x) for x in list_a]` – 001 Oct 23 '19 at 13:14
  • Maybe I misunderstood. Do you want a new list, or a string "362,673,196,452,972"? That would be `','.join(str(x) for x in list_a)` – 001 Oct 23 '19 at 13:17
  • In this case, you can use `",".join([str(x) for x in list_a])` – AlexandreS Oct 23 '19 at 13:20
  • Possible duplicate of [Concatenate item in list to strings](https://stackoverflow.com/questions/12453580/concatenate-item-in-list-to-strings) – mkrieger1 Oct 23 '19 at 13:21
  • Possible duplicate of [python list of numbers converted to string incorrectly](https://stackoverflow.com/questions/6950605/python-list-of-numbers-converted-to-string-incorrectly) – TrebledJ Oct 23 '19 at 13:27

1 Answers1

0

Like Johnny mentions in his comment list comprehension is your friend.

list_a=[362,673,196,452,972]
list_b=[str(x) for x in list_a]
print(list_a)
# create new list of strings
print(list_b)
# join to a single string
print(','.join(list_b))
# or use print if you don't want a new list
print(*list_a,sep=',')

output:

[362, 673, 196, 452, 972]
['362', '673', '196', '452', '972']
362,673,196,452,972
362,673,196,452,972
corn3lius
  • 4,857
  • 2
  • 31
  • 36