##### Flattening List
 Flattening List mp3909 Not Blown Up Yet Posts: 66 Threads: 28 Joined: Aug 2017 Reputation: Jan-25-2018, 08:45 PM Just a curious question, how would one flatten the following list `[[[2], [4, 1, 3]], [5, 7], 1, 10, [[[5, 5, 5], [4, 4, 4]]]]` ? Reply league55 Not Blown Up Yet Posts: 70 Threads: 23 Joined: Jan 2018 Reputation: Jan-25-2018, 08:51 PM I think with zip(). Reply Posts: 3,080 Threads: 44 Joined: Jan 2018 Reputation: Jan-25-2018, 09:38 PM (This post was last modified: Jan-25-2018, 09:38 PM by Gribouillis.) Here is a recursive solution ```from itertools import chain def iflatten(seq): return chain.from_iterable( iflatten(x) if isinstance(x, list) else (x,) for x in seq) def flatten(seq): return list(iflatten(seq)) if __name__ == '__main__': L = [[[2], [4, 1, 3]], [5, 7], 1, 10, [[[5, 5, 5], [4, 4, 4]]]] print(flatten(L))`````Output:[2, 4, 1, 3, 5, 7, 1, 10, 5, 5, 5, 4, 4, 4]`` Reply ka06059 Not Blown Up Yet Posts: 54 Threads: 0 Joined: Jan 2018 Reputation: Jan-25-2018, 10:46 PM (This post was last modified: Jan-25-2018, 10:49 PM by ka06059.) ```def flatten(list): flatted = [] for element in list: if type(element) != type([]): flatted+=[element] else: flatted+=flatten(element) return flatted A = [[[2], [4, 1, 3]], [5, 7], 1, 10, [[[5, 5, 5], [4, 4, 4]]]] flatten(A)```codes with basic keywords and list operation only Reply Posts: 591 Threads: 26 Joined: Sep 2016 Reputation: Jan-25-2018, 11:17 PM (This post was last modified: Jan-25-2018, 11:17 PM by Mekire.) Black magic: ```>>> a = [[[2], [4, 1, 3]], [5, 7], 1, 10, [[[5, 5, 5], [4, 4, 4]]]] >>> b = eval(repr(a).replace("[","").replace("]","")) >>> b (2, 4, 1, 3, 5, 7, 1, 10, 5, 5, 5, 4, 4, 4) >>>```But, in all seriousness, don't do that. `eval` is bad, you say? I know: ```>>> a = [[[2], [4, 1, 3]], [5, 7], 1, 10, [[[5, 5, 5], [4, 4, 4]]]] >>> b = list(map(int, repr(a).replace("[","").replace("]","").split(","))) >>> b [2, 4, 1, 3, 5, 7, 1, 10, 5, 5, 5, 4, 4, 4] >>>```Don't do this either. Reply Posts: 3,080 Threads: 44 Joined: Jan 2018 Reputation: Jan-25-2018, 11:18 PM (Jan-25-2018, 11:14 PM)Mekire Wrote: Black magic:There will be some trouble if the list contains literal strings instead of integers! Reply Posts: 591 Threads: 26 Joined: Sep 2016 Reputation: Jan-25-2018, 11:25 PM (Jan-25-2018, 11:18 PM)Gribouillis Wrote: (Jan-25-2018, 11:14 PM)Mekire Wrote: Black magic: There will be some trouble if the list contains literal strings instead of integers!The dark arts can be cruel and unforgiving. Reply league55 Not Blown Up Yet Posts: 70 Threads: 23 Joined: Jan 2018 Reputation: Jan-26-2018, 12:00 AM Teaching the Qliphoth is forbidden, my friends. Reply Posts: 5,520 Threads: 112 Joined: Sep 2016 Reputation: Jan-26-2018, 12:13 AM I want to break Python 2 support bye using `yield from`. ```def flatten(seq): for item in seq: if isinstance(item, list): yield from flatten(item) else: yield item lst = [[[2], [4, 1, 3]], [5, 7], 1, 10, [[[5, 5, 5], [4, 4, 4]]]] print(list(flatten(lst)))`````Output:[2, 4, 1, 3, 5, 7, 1, 10, 5, 5, 5, 4, 4, 4]`` Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Flattening attribute access ruy 5 332 Jun-25-2021, 08:26 PM Last Post: ruy Problem in flattening list Shahmadhur13 5 913 May-03-2020, 12:40 AM Last Post: DeaD_EyE flattening a list with some elements being lists Skaperen 17 3,276 Apr-09-2019, 07:08 AM Last Post: perfringo

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020