DEV Community

Cover image for Python challenge_16
Mahmoud EL-kariouny
Mahmoud EL-kariouny

Posted on

Python challenge_16

Writing short code

Define a function named convert that takes a list of numbers
as its only parameter and returns a list of each number converted to a string.

For example:

the call convert([1, 2, 3]) should return ["1", "2", "3"].

What makes this tricky is that your function body must only contain a single line of code.

Task URI

My solution

def convert(lists): return [str(con_list) for con_list in lists]
print(convert([1, 2, 3]))
Enter fullscreen mode Exit fullscreen mode

Another solution

def convert(ns):
    return [str(n) for n in ns]
Enter fullscreen mode Exit fullscreen mode

using map

def convert(ns):
    return list(map(str, ns))
Enter fullscreen mode Exit fullscreen mode

All the best to you.

Top comments (0)