How to join lists inside two or more different lists in arranged order?

Question:

I have three lists as follows.

A = [1, 2, 3]; B = [[3, 4, 5], [4, 5, 6], [4, 5, 7], [7, 4, 3]]; C = [[2, 3, 1], [2, 3, 3], [2, 4, 5], [4, 5, 6], [7, 3, 1]]

I want to create another list containing all the above inner lists starting from A to C.

Desired = [elements of A, elements of B, elements of C] just like this.

Desired = [[1, 2, 3], [3, 4, 5], [4, 5, 6], [4, 5, 7], [7, 4, 3], [2, 3, 1], [2, 3, 3], [2, 4, 5], [4, 5, 6], [7, 3, 1]]

Asked By: iPatrick

||

Answers:

The method I use below is to see if the inner_list contents are infact a list of themselves.

  • If they are, then append the inner list.
  • If they are not, then append the outer_list.
A = [1, 2, 3]; 
B = [[3, 4, 5], [4, 5, 6], [4, 5, 7], [7, 4, 3]]; 
C = [[2, 3, 1], [2, 3, 3], [2, 4, 5], [4, 5, 6], [7, 3, 1]]

desired = []

for outer_list in (A,B,C):
    list_state = False
    for inner_list in outer_list:
        if isinstance(inner_list, list):
            desired.append(inner_list)
        else:
            list_state = True
    
    # Add outer_list only if the inner_list was not a list
    if list_state:
        desired.append(outer_list)
        
print(desired)

OUTPUT:

[[1, 2, 3], [3, 4, 5], [4, 5, 6], [4, 5, 7], [7, 4, 3], [2, 3, 1], [2, 3, 3], [2, 4, 5], [4, 5, 6], [7, 3, 1]]
Answered By: ScottC

I made something to make a list to an nested list if it is not.

A = [1, 2, 3]
B = [[3, 4, 5], [4, 5, 6], [4, 5, 7], [7, 4, 3]]
C = [[2, 3, 1], [2, 3, 3], [2, 4, 5], [4, 5, 6], [7, 3, 1]]
listt = [ 'A', 'B', 'C' ]
des = []

for i in listt:
    if type((globals()[i])[0]) != list:
        globals()[i] = [[i for i in globals()[i]]] #turns into nested list if not (line 7-9)

for i in (A,B,C):
    for x in i:
        des.append(x)

print(des)

Output:

[[1, 2, 3], [3, 4, 5], [4, 5, 6], [4, 5, 7], [7, 4, 3], [2, 3, 1], [2, 3, 3], [2, 4, 5], [4, 5, 6], [7, 3, 1]]
Answered By: Jethy11

It seems like you know which list is nested (B and C) and which isn’t (A). If that’s the case then you could simply do:

result = [A] + B + C

If you don’t know but can determine the nestedness by looking at the first item, then you could do:

result = [
    item
    for numbers in (A, B, C)
    for item in (numbers if isinstance(numbers[0], list) else [numbers])
]

If that’s also not the case, i.e. there could be mixed lists (lists that contain numbers and lists), then you could use itertools.groupby from the standard library:

from itertools import groupby

result = []
for key, group in groupby(A + B + C, key=lambda item: isinstance(item, list)):
    if not key:
        group = [[*group]]
    result.extend(group)

Results for all versions (with A, B and C like provided):

[[1, 2, 3], [3, 4, 5], [4, 5, 6], [4, 5, 7], [7, 4, 3],
 [2, 3, 1], [2, 3, 3], [2, 4, 5], [4, 5, 6], [7, 3, 1]]
Answered By: Timus
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.