-1

Consider the following lists

list_1 = [[1,2],[3,4],[5,6]]
list_2 = [[7,8],[9,10],[11,12]]
list_3 = [[13,14],[15,16],[17,18]]

Is it possible to flatten all the lists in one go

i tried using

list_1 = [ y for x in list_1 for y in x]

this does only one list at a time. Any way to flatten all three lists in a single line of code?

the_reaper
  • 73
  • 6
  • Why do you want to flatten 3 different lists in one line of code? Why not use 3? – Joshua Nixon Jun 06 '19 at 21:58
  • because i have lot of lists and its repititive – the_reaper Jun 06 '19 at 22:00
  • 1
    Throwing everything onto one line is never better than writing multiple lines of code. Perhaps store the lists in another collection and loop over the collection and do the flatten – Joshua Nixon Jun 06 '19 at 22:02
  • Look for a recursive solution here: https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists or apply the top voted answer in a loop to each list you have. – cs95 Jun 06 '19 at 22:05
  • 2
    It could be done with `list_1, list_2, list_3 = [[ y for x in list_n for y in x] for list_n in [list_1, list_2, list_3]]` but it's a pretty ridiculous piece of code. – David Buck Jun 06 '19 at 22:11
  • @cs95 my question is different from the one you have provided. This is not a duplicate question! – the_reaper Jun 06 '19 at 22:16
  • 1
    trust me, I've seen this question hundreds of times. Your answer is [here](https://stackoverflow.com/a/40857703/4909087). – cs95 Jun 06 '19 at 22:17
  • @cs95 The OP has already shown they know how to flatten *a* list; they are asking how to flatten *multiple* lists at once. The answer you link to doesn't address that. – chepner Jun 06 '19 at 22:22
  • @chepner my understanding was that they wanted to "flatten all the lists in one go" meaning flatten it into a single list, although the other, more likely interpretation would've been to call a flatten function over each list in a loop, which they could've easily extended from the duplicate. Another user even posted a comment with the code to do so. I don't think you needed to reopen this. – cs95 Jun 06 '19 at 22:24
  • @the_reaper What end result do you want? Do you want `list_1 = [1,2,3,4,5,6]`, `list_2 = [7,8,9,10,11,12]`, etc, or do you want a single list `list_1 = [1,2,3,4,5,6,7,8,...,16,17,18]`? – chepner Jun 07 '19 at 00:39

2 Answers2

1

This can easily be done using itertools.chain()

itertools.chain(list1, list2, list3)

If you are only worried about lines, then why not this:

list1, list2, list3 = ["A","B","C"], [1,2,3], ["E","F","G"]
Aero Blue
  • 518
  • 2
  • 14
0

If you had a list of lists to flatten, you could use a loop:

list_1 = [[1,2],[3,4],[5,6]]
list_2 = [[7,8],[9,10],[11,12]]
list_3 = [[13,14],[15,16],[17,18]]

lists = [list_1, list_2, list_3]

def flatten(lst):
    ...

lists = [flatten(lst) for lst in lists]

Your problem is that you are trying to generalize something that can't be generalized: a set of independent variables. Ignoring unrecommended solutions involving dynamic variable names corrections, what you should do is simply enumerate the flattened lists:

list_1 = flatten(list_1)
list_2 = flatten(list_2)
list_3 = flatten(list_3)
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    "Your problem is that you are trying to generalize something that can't be generalized" Why not? As long as they have a manageable number of lists, `list_1, list_2, list_3 = [flatten(lst) for lst in [list_1, list_2, list_3]]` would be sufficient. – cs95 Jun 06 '19 at 22:29
  • "because i have lot of lists and its repititive". I suspect there are far more than 3 lists in the OP's actual code. – chepner Jun 07 '19 at 00:39