How to create a unique list of lists from a list of list of lists without "flattening"

Question:

Edited Question

I’ve got an inner nested list as shown below.

test_list = [
    [['1']], 
    [['2', '2']], 
    [['3', '3'], ['4', '4'], ['5', '5'], ['6', '6']], 
    [['7', '7'], ['8'], ['7'], ['1'], ['7', '7']], 
    [['7', '7'], ['8'], ['7'], ['7', '7']], 
    [['2', '2']]]

I’m not looking out to flatten this list per-say. I would rather like to achieve two results.

1)Return a list which discards the outer list for indexes with single item, but keep the nested list for indexes with grouped items. i.e

Expected list output:

new_list = [
    ['1'], 
    ['2', '2'], 
    [['3', '3'], ['4', '4'], ['5', '5'], ['6', '6']], 
    [['7', '7'], ['8'], ['7'], ['1'], ['7', '7']], 
    [['7', '7'], ['8'], ['7'], ['7', '7']], 
    ['2', '2']]

And when printed looks like:

['1'] 
['2', '2']
[['3', '3'], ['4', '4'], ['5', '5'], ['6', '6']] 
[['7', '7'], ['8'], ['7'], ['1'], ['7', '7']] 
[['7', '7'], ['8'], ['7'], ['7', '7']] 
['2', '2']

2)Return a new unique list which would look like the new_list variable expressed below.

new_list = [
    ['1'], 
    ['2'], 
    ['3', '4', '5', '6'], 
    ['7', '8', '7', '1', '7'], 
    ['7', '8', '7', '7'], 
    ['2']]

i.e each item in the new list printed out as:

['1']
['2']
['3', '4', '5', '6']
['7', '8', '7', '1', '7']
['7', '8', '7', '7']
['2']

Thanks.
PS: Sorry for the total re-edit, I totally misrepresented the expected result.

Asked By: ola

||

Answers:

You can use a simple test on the sublists length in a list comprehension:

out = [l[0] if len(l)==1 else l for l in test_list]

output:

[['1'],
 ['2', '2'],
 [['3', '3'], ['4', '4'], ['5', '5'], ['6', '6']],
 [['7', '7'], ['8'], ['7'], ['1'], ['7', '7']],
 [['7', '7'], ['8'], ['7'], ['7', '7']],
 ['2', '2']]

If you really want to print:

for l in test_list:
    print(l[0] if len(l)==1 else l)

['1']
['2', '2']
[['3', '3'], ['4', '4'], ['5', '5'], ['6', '6']]
[['7', '7'], ['8'], ['7'], ['1'], ['7', '7']]
[['7', '7'], ['8'], ['7'], ['7', '7']]
['2', '2']

edited question

you need a nested list comprehension:

[[x[0] for x in l] for l in test_list]

Output:

[['1'],
 ['2'],
 ['3', '4', '5', '6'],
 ['7', '8', '7', '1', '7'],
 ['7', '8', '7', '7'],
 ['2']]
Answered By: mozway

If you struggle with understanding list comprehension, especially the nested list comprehension, you can break it down with the usual for loop and if’s conditional.

new_list = []
for each_index in test_list:
    if len(each_index) == 1:
        new_list.append(each_index[0])
    else:
        new_list.append(each_index)
print(new_list)

And the second answer with the nested list comprehension translates to:

new_list = []
for each_index in test_list:
    test = []
    for each_item in each_index:
        test.append(each_item[0])
    new_list.append(test)
print(new_list)

NOTE:
The list comprehension answer given by @mozway is more concise, but when I want to properly understand the flow of list comprehension, a good practice is to break it down like this so that i avoid just copying and pasting, but leave with a better understanding.

Answered By: ola
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.