-1

I have a list-tuple-list tree and I'd like to flatten it for a query, so that I can print all the items of the the first lists of each tuple.

I can do it via a for loop:

bigNest = [([item1,item2],[]),([item3],[item4])]
mergedlist = []
for listItem in bigNest:
   mergedlist += listItem[0]
print mergedList

Is there a simpler/quicker way which will also work with larger tuples?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Db0
  • 151
  • 2
  • 10
  • 7
    Have you done a search for "flatten"? This has been answered 100 times. – Mark Ransom Nov 30 '12 at 17:45
  • There are a lot of different ways to do, as discussed in the previous answers (which is why I'm voting to close). There's terse but slow (`sum(nested_list, [])`) or the various itertools-based solutions (e.g. `itertools.chain`) which will scale up considerably better. – Joe Kington Nov 30 '12 at 17:50
  • I did actually search but I obviously didn't hit the right keywords because none of the topics above came up – Db0 Nov 30 '12 at 17:53
  • Also, sorry but none of the "possible duplicates" above answer my question. I know how to flatten if I want to. I want the pythonic way to do it. The ones listed are all bigger than mine! – Db0 Nov 30 '12 at 17:56
  • 1
    `print list(chain.from_iterable(item for item in chain.from_iterable(bigNest) if item))` –  Nov 30 '12 at 18:01
  • None of them explicitly answer the exact same question (you only want to flatten the first item, instead of flattening recursively). However, you can use the exact same tricks. E.g. `merged = sum([item[0] for item in nested], [])` – Joe Kington Nov 30 '12 at 18:02
  • I'm surprised you didn't hit the right keyword, as you used "flatten" both in the title and question and it's very relevant and specific. The reason your answer is shorter is because it's a specific subset of the general problem, where you're only merging a particular nesting level. – Mark Ransom Nov 30 '12 at 18:09
  • You are probably right but that's why I asked! Unfortunately I'm not that good with python to be able to figure if code made for something different will work with what I need to do. – Db0 Nov 30 '12 at 18:11
  • @JoeKington unfortunately, that didn't work. It ended up duplicating the content. – Db0 Nov 30 '12 at 19:07
  • You might have succeeded in asking the question if you pointed out some of the other questions/answers and why they were not sufficient. – Mark Ransom Nov 30 '12 at 19:09
  • I honestly just wanted a quick question. I didn't want to write a whole essay. I saw someone asking how to simply combine lists and they got an answer so I thought me putting an existing solution and asking for a more refined one would be enough. Forget it... – Db0 Nov 30 '12 at 19:15
  • "which will also work with larger tuples?" It's not clear what the intended concern was. `listItem[0]` will work no matter how many elements are in `listItem`, *as long as there is at least one*. – Karl Knechtel Sep 06 '22 at 06:10

1 Answers1

1

edit: sorry the last post was wrong, you could do a list comprehension:

mergedlist = []
foovar = [mergedlist.extend(i[0]) for i in bigNest]
Cameron Sparr
  • 3,925
  • 2
  • 22
  • 31