Skip to content
Advertisement

how to remove the above elements from list from matrix by python [closed]

matrixA = 
[['AAA', 'BBB', 'CCC'],
['PPP', 'QQQ', 'RRR', 'SSS'],
['DDD','EEE','FFF'],
['GGG', 'HHH', 'III']]

how to remove the above elements from listA from matrixA

listA = ['DDD', 'EEE', 'FFF']

Desired output:

output_matrix = 
[['DDD','EEE','FFF'],
['GGG', 'HHH', 'III']]

sorry for my bad language

Advertisement

Answer

You can get the index of listA within matrixA and then slice with it:

idx = matrixA.index(listA)
out = matrixA[idx:]

to get

>>> out

[["DDD", "EEE", "FFF"], ["GGG", "HHH", "III"]]

in case listA doesn’t exist, it gives an error so we try:

try:
    idx = matrixA.index(listA)
except ValueError:
    # if it doesn't exist..
    out = matrixA          # we can take the whole `matrixA`, for example
else:
    # it does exist
    out = matrixA[idx:]
Advertisement