Reputation: 41
I have a list a=['1','2',3,4,5,6]
How do i get the list a=['1','2','3','4','5','6']
?
I have tried using str
. but doesn't work
Upvotes: 0
Views: 867
Reputation: 11228
a=['1','2',3,4,5,6]
a=[str(a[i]) for i in range(len(a))]
output
['1', '2', '3', '4', '5', '6']
Upvotes: 1
Reputation: 169
your code is ok, but str is a build-in function, you may have renamed str to some value, this will got a err.
>>> str = 'str'
>>> str('1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
Upvotes: 1
Reputation: 325
You need to take each element and convert it into string. Converting the whole list at once(like a=str(a)
), won't work.
for idx, element in enumerate(a):
a[idx] = str(element)
Upvotes: 1
Reputation: 11
List comprehension:
a = [str(x) for x in a]
Map:
a = list(map(str, a))
Upvotes: 1