2

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

5 Answers 5

4

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']]
Sign up to request clarification or add additional context in comments.

Comments

3
lst = [["asdf","bmnl", "123","456","0","999","1234","3456"],["qwer","tyui","789","657","122","9","673","1"]]
for i in lst:
    del i[0:2] #deleting 0 and 1 index from each list

print lst

Comments

2

This is a typical use case for a list comprehension:

list2 = [item[2:] for item in list1]

Comments

2

You can use a list comprehension like below:

[item[2:] for item in my_list]

item[2:] called list slicing, and it means that for each sub-list of my_list, we take items from the index 2 till the last item.

Output:

>>> my_list = [["asdf", "bmnl", "123", "456", "0", "999", "1234", "3456"], ["qwer", "tyui", "789", "657", "122", "9", "673", "1"]]
>>>
>>> [item[2:] for item in my_list]
[['123', '456', '0', '999', '1234', '3456'], ['789', '657', '122', '9', '673', '1']]

Comments

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

list_1, list_2 = [i[2:] for i in start] # I use list comprehension to create a multidimensional 
                                        # list that contains slices of each object in the base 
                                        # list and unwrap it to list_1 and list_2

Same as

n_list = [] #create an empty new list
for i in start: #loop through the origional
    n_list.append(i[2:]) #append slices of each item to the new list
list_1, list_2 = n_list #unwrap the list

1 Comment

Please include some explanation to go with code so others can understand how it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.