Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"]
like image 474
user02 Avatar asked Oct 24 '25 23:10

user02


1 Answers

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.

like image 142
Elisha Avatar answered Oct 26 '25 13:10

Elisha