U31Vallurie U31Vallurie - 3 months ago 8
Python Question

How to convert a list of lists containing a single integer each into a list of integers?

Here is an example of a list of lists containing a single integer each:

arr = [[1],[2],[3],[4]]


I would like to convert the above list into a list of integers:

arr = [1,2,3,4]


Any suggestions? Thanks.

Answer

A simple list comprehension can do this.

arr = [i[0] for i in arr]

You can use map also.

arr =  map(lambda x:x[0], arr)