0
a = [
[1, 2],
[1, 2]
]

print([x for x in [x for x in a]])
[[1, 2], [1, 2]]

But I want to see something like that:

[1, 2, 1, 2]

How I cad do this?

2 Answers2

1

Try this.

a = [
[1, 2],
[1, 2]
]

lst = [q for i in a for q in i]
print(lst)

The above one is not always working it gives you an error if the list is something like this.

a = [[1, 2],[1, 2],2]

But this one always work.

a = [
[1, 2],
[1, 2]
]
lst = []
for i in a:
    if type(i) == list:
        lst.extend(i)
    else:
        lst.append(i)
print(lst)
codester_09
  • 5,622
  • 2
  • 5
  • 27
0
    a = [
[1, 2],
[1, 2]
]
flatten_list = lambda y:[x for a in y for x in flatten_list(a)] if type(y) is list else [y]

print(flatten_list(a))