How To Convert Nested List Of Numbers To List Of Strings?
I have a list of lists below p=[[1,2,3,4],[2,3,4,1]] How do I put the sublist into a string? For example, desire result is: p=['1234','2341']
Solution 1:
It can be done by converting every integer to string and joining the strings:
p = [''.join(map(str, sub_list)) for sub_list in p] # ['1234', '2341']
Ever every nested list, like [1, 2, 3, 4]
, map(str, [1, 2, 3, 4])
would create a list of strings. In this example: ['1', '2', '3', '4']
. Using the join function, the string-numbers are mapped into a single string, '1234'
.
Since this operation is performed for every sub-list, the result is a a list of the joined strings.
Post a Comment for "How To Convert Nested List Of Numbers To List Of Strings?"