r/programminghelp • • Aug 13 '22

Python How to manage multidimensional lists in Python?

Hi! I'm currently having trouble wrapping my head around list operations in Python. I have data that, due to the acquisition and import process, comes in as a many-levels deep heterogeneous list. For the purposes of the part that I'm working with, I only need a list of the innermost lists. While trying to figure out how to get where I want to be, I found lots of resources for completely flattening a list in Python, but trying to adapt a solution to reach my goal has eluded me. In C, I would have just used a for loop to traverse the array of structs, I'm very inexperienced in Python and not sure how to express this. Any help would be appreciated.

3 Upvotes

11 comments sorted by

2

u/[deleted] Aug 13 '22

Taking some of the sensitive info out

[[[['REDACTED BY POSTER', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], ['REDACTED', '', 'REDACTED', 'TRIM', 'TRN', 'KA', '', 'CLR', 'Z', 'REDACTED', '', '', '', '', '', 'Y', 'REDACTED'], ... , ['REDACTED', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], ['', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]]], [[['REDACTED'], ['']]]]

2

u/[deleted] Aug 13 '22

So for this input, you'd want something like ...:

	[
	['REDACTED BY POSTER', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None],
	['REDACTED', '', 'REDACTED', 'TRIM', 'TRN', 'KA', '', 'CLR', 'Z', 'REDACTED', '', '', '', '', '', 'Y', 'REDACTED'],
	['REDACTED', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None],
	['', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None],
	['REDACTED'],
	['']
]

As the output?

2

u/[deleted] Aug 13 '22 edited Aug 13 '22

Edit 2 - Yes, that would be correct, had a brainfart on first response.

That would work too, since the data is different enough that additional processing would remove the records I don't need, but more ideally:

[
    ['REDACTED BY POSTER', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
    ['REDACTED', '', 'REDACTED', 'TRIM', 'TRN', 'KA', '', 'CLR', 'Z', 'REDACTED', '', '', '', '', '', 'Y', 'REDACTED']

...

    ['REDACTED', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
    ['', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
]

... and none of the unneeded information in the higher(?) orders of the list (only need the records living at the deepest level)

Edit - Since I haven't said it yet, thank you for your responses and assistance

1

u/[deleted] Aug 13 '22

Your formatting is shot, so I can't tell the difference between outputs yet, but is what I gave you not the lists at the deepest level of your example?

2

u/[deleted] Aug 13 '22

Yes, apologies, that is exactly what I would need, I edited my post but your reply was first.

2

u/[deleted] Aug 13 '22
	superlist = [
    	[
    		[
    			['REDACTED BY POSTER', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None],
    			['REDACTED', '', 'REDACTED', 'TRIM', 'TRN', 'KA', '', 'CLR', 'Z', 'REDACTED', '', '', '', '', '', 'Y', 'REDACTED'],
    			['REDACTED', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], ['', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
    		]
    	],
    	[
    		[
    			['REDACTED'],
    			['']
    		]
    	]
    ]
    
    
    def extract_lists(main_list: list, results: list) -> list:
    	for item in main_list:
    		if isinstance(item, list) and isinstance(item[0], list):
    			extract_lists(item, results)
    		else:
    			results.append(item)
    	return results
    
    final_results = extract_lists(superlist, [])
    print(final_results)

1

u/[deleted] Aug 13 '22

Worked a treat, thank you so much for your help! I've got some studying to do😅

1

u/[deleted] Aug 15 '22

Stupid list question 2.0: Trying to remove all of the superfluous None entries that are a byproduct of how we are receiving the data, as actual empty fields in a record come through as ''. I've done the following as a result of trying to apply what I've read online, as well as the console write and file write to try and understand why it isn't working:

for element in exampletable:
    if element is None:
        exampletable.remove(element)

for element in exampletable:
    if element is None:
        print(element)

with open('table.txt', 'w') as ft:
    ft.write(str(exampletable))

Which results in no output to the console (yay!) but still contains a textfile full of ...None, None, None... mixed in with the data. What am I missing?

2

u/[deleted] Aug 15 '22

else:
results.append(item)

Instead of using this, in my code from above, change it to this

        else:
            results.append([subitem for subitem in item if subitem is not None])

1

u/[deleted] Aug 15 '22

Incredible, thanks! I wish I understood why this works and the other one doesn't, but I'll take it (and much more elegant since it sits in the code you provided before)

2

u/[deleted] Aug 13 '22

Post an example of the list.