Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - removing items in multidimensional array

I have a multidimensional array such as this:

[["asdf","bmnl", "123","456,"0","999","1234","3456"],["qwer","tyui","789","657,"122","9","673","1"]]

However, in the multidimensional array, only the last 6items of each array are needed and the first two are not needed. How can I remove the first two pieces of data from each of the arrays within the multidimensional array so it would look like:

[["123","456,"0","999","1234","3456"],["789","657,"122","9","673","1"]]

So far, I have done this:

list1 = []
list2 = []
for row in rows:
    list1.append(row[0].split(',')) #to put the split list into the top i i.e. [["asdf","bmnl", "123","456,"0","999","1234","3456"]["qwer","tyui","789","657,"122","9","673","1"]]
for i in list1:
    for index in len(list1):
        if index >=2:
             list2.append(index) #this does not work and causes errors

How could I go about fixing this so the output would be:

[["123","456,"0","999","1234","3456"],["789","657,"122","9","673","1"]]

Thanks


1 Answers

Just use a list comprehension and grab every element from index 2 and beyond in each sublist:

>>> array = [["asdf","bmnl", "123","456","0","999","1234","3456"],["qwer","tyui","789","657","122","9","673","1"]]
>>> [sublist[2:] for sublist in array]
[['123', '456', '0', '999', '1234', '3456'], ['789', '657', '122', '9', '673', '1']]
like image 122
blacksite Avatar answered Mar 04 '26 22:03

blacksite