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 trial

Python

Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,408 Points

Function 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
Steven Parker
229,644 Points

Your 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]