4

for solving above problem I normally use for loop like that:

list1 = ['1', '2', 3,32423,5.76, "afef"]
list2 = []
for ele in list1:
  list2.append(str(ele))

does python has some build in function for solving that problem?

1
  • use list comprehensions eg [str(i) for i in list1] Commented Jul 31, 2020 at 2:08

1 Answer 1

9

You can use the built-in function map like the following:

list1 = ['1', '2', 3, 32423, 5.76, "afef"]
list2 = list(map(str, list1))
Sign up to request clarification or add additional context in comments.

3 Comments

for this simple solution even list2=list1 will also work. Correct me if I am wrong on this.
@ArunbhYashaswi What do you mean? list1 contains numbers like 3, 32423, 5.76, but list2 contains only strings. list1 == ['1', '2', 3, 32423, 5.76, 'afef'] while list2 == ['1', '2', '3', '32423', '5.76', 'afef'].
@SwayWu Happy to hear that! You can also accept my answer if you want to :) You can refer: stackoverflow.com/help/someone-answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.