Python Forum
help with flatten nested list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with flatten nested list
#1
I am trying to flatten my nested list

Input:

list1=[[[0],[1400,100]],[[0],[1200,100]],[[100],[800,400]]]

Expected output:

flattened1=[[0,1400,100],[0,1200,100],[100,800,400]]

Any ideas on how to achieve the outcome aboove?

Many thanks
Reply
#2
What have you tried? We're not going to write your code for you, but we would be happy to help you fix your code when you run into problems. When you do run into problems, be sure to post your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
you can have a look at the code below. It does the thing that I need, but I was wondering if there is anohter way to acheive the same outcome with fewer lines of codes or maybe less iteration through using python modules. any ideas on how to acheive the same output?

list1=[[[0],[1400,100]],[[0],[1200,100]],[[100],[800,400]]]




flattened_list = []
for x in list1:
    flattened_list.append([])
    for y in x:
        if type(y) is list:
            for z in y:
                flattened_list[-1].append(z)
        else:
            flattened_list[0].append(y)


print (flattened_list)
Reply
#4
Just a nitpicking, but there is subtle difference between flat list and list with flattened elements in it (a.k.a list of lists; two-dimensional list; matrix).

>>> list_of_lists = [[0, 1400, 100], [0, 1200, 100], [100, 800, 400]] 
>>> flat_list = [0, 1400, 100, 0, 1200, 100, 100, 800, 400]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
@perfringo

Please have a look at the post above. I know the difference between flattened list and nested list. but in the example above as you can see i am aiming for flattened nested list which is a combination of nested list flatten at specific index. Feel free to post another code that get me the result I am seeking.
Reply
#6
Specific conditions are unknown to me therefore I assume, that code must deliver result only in this particular case. This is the shortest I am able to write:

>>> list1=[[[0],[1400,100]],[[0],[1200,100]],[[100],[800,400]]] 
>>> [sum(row, []) for row in list1]
[[0, 1400, 100], [0, 1200, 100], [100, 800, 400]]
In slightly longer form:

>>> list1=[[[0],[1400,100]],[[0],[1200,100]],[[100],[800,400]]] 
>>> [[el for lst in row for el in lst] for row in list1]
[[0, 1400, 100], [0, 1200, 100], [100, 800, 400]]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
Note that each sub-list follows the same pattern, and there are always two sub-sub-lists.

[a + b for a, b in list1]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List all possibilities of a nested-list by flattened lists sparkt 1 911 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Updating nested dict list keys tbaror 2 1,271 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Python Program to Find the Total Sum of a Nested List vlearner 8 4,886 Jan-23-2022, 07:20 PM
Last Post: menator01
  Looping through nested elements and updating the original list Alex_James 3 2,111 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  shuffle a nested list giorgosmarga 11 11,886 Nov-12-2020, 07:04 PM
Last Post: perfringo
Question Save list with nested list into CSV SpongeB0B 1 5,342 Oct-12-2020, 07:26 AM
Last Post: bowlofred
  Struggling with nested list gr3yali3n 3 2,290 Jul-09-2020, 05:30 PM
Last Post: DPaul
  Flatten list sublists chesschaser 8 2,875 Jul-07-2020, 11:38 AM
Last Post: chesschaser
  Nested Dictionary/List tonybrown3 5 3,137 May-08-2020, 01:27 AM
Last Post: tonybrown3
  Help removing asterisk item in a nested list. bmcguire 3 2,581 Apr-06-2020, 02:35 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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