Search code examples
pythonlistsublist

Breaking a list into two sublists with Python


Here is the list I want to break down:

listA = [[[0, 3], [1, 2]],
 [[0, 2], [1, 3]],
 [[0, 3], [1, 2]],
 [[0, 1], [2, 3]],
 [[0, 2], [1, 3]],
 [[2, 3], [0, 1]],
 [[1, 2], [0, 3]],
 [[2, 3], [0, 1]],
 [[2, 3], [0, 1]],
 [[1, 2], [0, 3]],
 [[1, 2], [0, 3]],
 [[1, 2], [0, 3]],
 [[1, 2], [0, 3]],
 [[2, 3], [0, 1]],
 [[0, 2], [1, 3]],
 [[2, 3], [0, 1]],
 [[2, 3], [0, 1]],
 [[0, 2], [1, 3]],
 [[0, 2], [1, 3]],
 [0, 2],
 [0, 2],
 [0, 2],
 [2, 3],
 [0, 2],
 [0, 2],
 [2, 3],
 [2, 3],
 [2, 3],
 [2, 3],
 [2, 3],
 [[0, 3], [1, 2]],
 [[0, 3], [1, 2]],
 [2, 3],
 [0, 2],
 [0, 2],
 [0, 2],
 [0, 2],
 [1, 2],
 [1, 2]]

I'm wondering if I could create two lists, where the first list contains all the sublists of A with their sublists (such as [[0, 3], [1, 2]], [[2, 3], [0, 1]], etc) while the second list contains the sublists of A only have numbers (such as [1,2], [2,3], etc). How can I do that? Thanks for the help:)


Solution

  • You can iterate throught the list such as:

    listA = [[[0, 3], [1, 2]], [[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 1], [2, 3]], [[0, 2], [1, 3]], [[2, 3], [0, 1]], [[1, 2], [0, 3]], [[2, 3], [0, 1]], [[2, 3], [0, 1]], [[1, 2], [0, 3]], [[1, 2], [0, 3]], [[1, 2], [0, 3]], [[1, 2], [0, 3]], [[2, 3], [0, 1]], [[0, 2], [1, 3]], [[2, 3], [0, 1]], [[2, 3], [0, 1]], [[0, 2], [1, 3]], [[0, 2], [1, 3]], [0, 2], [0, 2], [0, 2], [2, 3], [0, 2], [0, 2], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [[0, 3], [1, 2]], [[0, 3], [1, 2]], [2, 3], [0, 2], [0, 2], [0, 2], [0, 2], [1, 2], [1, 2]]
    
    listAux1 = list()
    listAux2 = list()
    
    for item in listA:
        if type(item[0]) == list:
            listAux1.append(item)
        else:
            listAux2.append(item)
    
    print(listAux1)
    print("------")
    print(listAux2)
    

    Hope I have answered your question!