-2

I have a list of lists of integers like this:

x = [[1], [2], [3]]

How do I convert it to a list of integers:

x = [1, 2, 3]

I was wondering if there are other ways to do it besides a for loop. Thanks!

amanda
  • 1
  • 1
  • 2
    See this: [How to make a flat list out of a list of lists?](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists). – Have a nice day May 31 '21 at 21:28
  • Why don't you want to use a for loop? – John Gordon May 31 '21 at 21:29
  • @JohnGordon: see "How to make a flat list out of a list of lists?" (https://stackoverflow.com/a/952952/4906636) per Haveaniceday's suggestion. – Cbhihe May 31 '21 at 21:34

1 Answers1

0

I would go for using .extend() if you don't want to use a double for loop.

y = []
for small in x:
    y.extend(small)

But if a double for loop is fine for you then this could work too

y = [num for sub in x for num in sub]

output

[1, 2, 3]
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44