I would like to remove a list from inside another list. I have a list which looks like this:
[[1, 2, 3, 4, 5, 6]]
I would like the list to look like this, so it is only a singular list rather than nested:
[1, 2, 3, 4, 5, 6]
I would like to remove a list from inside another list. I have a list which looks like this:
[[1, 2, 3, 4, 5, 6]]
I would like the list to look like this, so it is only a singular list rather than nested:
[1, 2, 3, 4, 5, 6]
To achieve the desired result, you can just access the zero index from original list.
a = [[1, 2, 3, 4, 5, 6]]
>>> b = a[0]
>>> b
// [1, 2, 3, 4, 5, 6]
If your list is :
lst = [[1,2,3,4,5,6]]
then you can do like this
new_lst = lst[0]
You new_lst
would be :
[1,2,3,4,5,6]
If your original lst has many lists inside it like :
lst = [[1,2,3,4,5,6],[7,8,9,10],...]
Then you can do the following :
new_lst = []
for i in lst :
for j in i :
new_lst.append(j)
So your new_lst
would be :
[1,2,3,4,5,6,7,8,9,10...]
You could also do it in a shorthand notation as :
new_list = [element for sublist in lst for element in sublist]
The above snippet is exactly similar as previous one, just a shorthand notation