Larry Freeman Larry Freeman - 2 years ago 69
Python Question

In python, is there a way to convert of list of integer-lists into a list of string-lists without creating a custom function?

Let's say that I have a list of integer-lists:


And I want to convert this into:


I could solve this issue by creating my own function. Is there a way to solve it without a custom function?

def stringify(x):
return map(str,x)

start = [[1,2,3],[4,5,6],[7,8,9]]

result = map(stringify,start)

Answer Source

To make it as pythonic as possible, I would write:

result = [[str(subitem) for subitem in sublist] for sublist in start]

IMO, it is always better to write the most readable code, and list-comprehensions are sometimes faster than map.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download