Skip to main content
Rollback to Revision 23
Source Link
Ry-
  • 226.3k
  • 56
  • 496
  • 504

I have a list of lists: like

[
    [1, 2, 3],
    [4, 5, 6],
    [7],
    [8, 9]
]

How can I flatten it to get [1, 2, 3, 4, 5, 6, 7, 8, 9]?


If your list of lists comes from a nested list comprehension, the problem can be solved more simply/directly by fixing the comprehension; please see How can I get a flat result from a list comprehension instead of a nested list?.

The most popular solutions here generally only flatten one "level" of the nested list. See Flatten an irregular (arbitrarily nested) list of lists for solutions that completely flatten a deeply nested structure (recursively, in general).

I have a list of lists:

[
    [1, 2, 3],
    [4, 5, 6],
    [7],
    [8, 9]
]

How can I flatten it to get [1, 2, 3, 4, 5, 6, 7, 8, 9]?

I have a list of lists like

[
    [1, 2, 3],
    [4, 5, 6],
    [7],
    [8, 9]
]

How can I flatten it to get [1, 2, 3, 4, 5, 6, 7, 8, 9]?


If your list of lists comes from a nested list comprehension, the problem can be solved more simply/directly by fixing the comprehension; please see How can I get a flat result from a list comprehension instead of a nested list?.

The most popular solutions here generally only flatten one "level" of the nested list. See Flatten an irregular (arbitrarily nested) list of lists for solutions that completely flatten a deeply nested structure (recursively, in general).

Removed answer from question.
Source Link
user4157124
  • 3k
  • 21
  • 33
  • 48

I have a list of lists like:

[
    [1, 2, 3],
    [4, 5, 6],
    [7],
    [8, 9]
]

How can I flatten it to get [1, 2, 3, 4, 5, 6, 7, 8, 9]?


If your list of lists comes from a nested list comprehension, the problem can be solved more simply/directly by fixing the comprehension; please see How can I get a flat result from a list comprehension instead of a nested list?.

The most popular solutions here generally only flatten one "level" of the nested list. See Flatten an irregular (arbitrarily nested) list of lists for solutions that completely flatten a deeply nested structure (recursively, in general).

I have a list of lists like

[
    [1, 2, 3],
    [4, 5, 6],
    [7],
    [8, 9]
]

How can I flatten it to get [1, 2, 3, 4, 5, 6, 7, 8, 9]?


If your list of lists comes from a nested list comprehension, the problem can be solved more simply/directly by fixing the comprehension; please see How can I get a flat result from a list comprehension instead of a nested list?.

The most popular solutions here generally only flatten one "level" of the nested list. See Flatten an irregular (arbitrarily nested) list of lists for solutions that completely flatten a deeply nested structure (recursively, in general).

I have a list of lists:

[
    [1, 2, 3],
    [4, 5, 6],
    [7],
    [8, 9]
]

How can I flatten it to get [1, 2, 3, 4, 5, 6, 7, 8, 9]?

added 50 characters in body
Source Link
buhtz
  • 12.5k
  • 22
  • 95
  • 196

I have a list of lists like [[1, 2, 3], [4, 5, 6], [7], [8, 9]].

[
    [1, 2, 3],
    [4, 5, 6],
    [7],
    [8, 9]
]

How can I flatten it to get [1, 2, 3, 4, 5, 6, 7, 8, 9]?


If your list of lists comes from a nested list comprehension, the problem can be solved more simply/directly by fixing the comprehension; please see How can I get a flat result from a list comprehension instead of a nested list?.

The most popular solutions here generally only flatten one "level" of the nested list. See Flatten an irregular (arbitrarily nested) list of lists for solutions that completely flatten a deeply nested structure (recursively, in general).

I have a list of lists like [[1, 2, 3], [4, 5, 6], [7], [8, 9]]. How can I flatten it to get [1, 2, 3, 4, 5, 6, 7, 8, 9]?


If your list of lists comes from a nested list comprehension, the problem can be solved more simply/directly by fixing the comprehension; please see How can I get a flat result from a list comprehension instead of a nested list?.

The most popular solutions here generally only flatten one "level" of the nested list. See Flatten an irregular (arbitrarily nested) list of lists for solutions that completely flatten a deeply nested structure (recursively, in general).

I have a list of lists like

[
    [1, 2, 3],
    [4, 5, 6],
    [7],
    [8, 9]
]

How can I flatten it to get [1, 2, 3, 4, 5, 6, 7, 8, 9]?


If your list of lists comes from a nested list comprehension, the problem can be solved more simply/directly by fixing the comprehension; please see How can I get a flat result from a list comprehension instead of a nested list?.

The most popular solutions here generally only flatten one "level" of the nested list. See Flatten an irregular (arbitrarily nested) list of lists for solutions that completely flatten a deeply nested structure (recursively, in general).

added 17 characters in body
Source Link
Karl Knechtel
  • 61.4k
  • 14
  • 132
  • 193
Loading
see-also the other important question in this topic space
Source Link
Karl Knechtel
  • 61.4k
  • 14
  • 132
  • 193
Loading
Shorten; promote my older comment into a see-also
Source Link
Karl Knechtel
  • 61.4k
  • 14
  • 132
  • 193
Loading
edited title
Link
Mateen Ulhaq
  • 27.9k
  • 21
  • 121
  • 155
Loading
Active reading.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134
Loading
More readable.
Source Link
Mateen Ulhaq
  • 27.9k
  • 21
  • 121
  • 155
Loading
Shorten. Added relevant example. Removed second sub-question about error which isn't addressed by answers.
Source Link
Mateen Ulhaq
  • 27.9k
  • 21
  • 121
  • 155
Loading
Ease of reading edit. Added source fences with language codes. Added punctuation to title.
Source Link
bad_coder
  • 13.2k
  • 20
  • 59
  • 95
Loading
Active reading.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134
Loading
Python 3 and shorten
Source Link
user3064538
user3064538
Loading
Improved formatting. Put question mark back into title.
Source Link
martineau
  • 124.1k
  • 29
  • 181
  • 319
Loading
Active reading.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134
Loading
Edited title to better express question being asked and removed language tag.
Link
martineau
  • 124.1k
  • 29
  • 181
  • 319
Loading
tag update
Link
dreftymac
  • 32.6k
  • 27
  • 125
  • 191
Loading
Rollback to Revision 6
Source Link
Taryn
  • 248.9k
  • 57
  • 374
  • 409
Loading
added 312 characters in body
Source Link
m87
  • 4.5k
  • 3
  • 18
  • 31
Loading
deleted 311 characters in body
Source Link
wim
  • 368.1k
  • 114
  • 681
  • 817
Loading
Post Reopened by Claudiu python
marking code, replace quote with code block
Source Link
xiº
  • 4.7k
  • 3
  • 31
  • 43
Loading
Question Protected by Ashwini Chaudhary
Quoted the error message.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134
Loading
insert duplicate link
Source Link
Loading