Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAsher Orr
Python Development Techdegree Graduate 9,409 PointsFunction goal: taking a list of numbers & returning a list of each number converted to a string (in one line only!)
Hi everyone! I need help with a function.
My function, convert, takes a list of numbers as its only parameter. It should return a list of each number converted to a string.
For example, the call convert([1, 2, 3]) should return ["1", "2", "3"].
Here's the tricky thing: the function body must contain only one line of code.
This is what I have so far:
def convert(list_nums):
return list("".join([str(x) for x in list_nums]))
When I call the function like this:
convert([1, 2, 3, 999])
.. and I print the result, I see this:
['1', '2', '3', '9', '9', '9']
When it should look like this:
['1', '2', '3', '999']
I am stumped on how I can manipulate the function to convert each number to a string, rather than each element of a number. Can anyone suggest how I might remedy this?
Thanks for reading!
1 Answer
Steven Parker
231,236 PointsYour list comprehension already does what you want, but then the individual strings get joined into one big one, and then each character is made into a list item.
Just do the comprehension by itself:
def convert(list_nums):
return [str(x) for x in list_nums]
Asher Orr
Python Development Techdegree Graduate 9,409 PointsAsher Orr
Python Development Techdegree Graduate 9,409 PointsPerfect. I appreciate you Steven!