0

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]

james1234
  • 13
  • 2
  • 1
    For this specific case, you can reassign to include only the inner list: `mylist = mylist[0]` – John Gordon May 28 '20 at 21:33
  • 3
    Does this answer your question? [How to make a flat list out of list of lists?](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists) – Alireza May 28 '20 at 21:34

2 Answers2

0

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]
Danizavtz
  • 3,166
  • 4
  • 24
  • 25
0

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

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32