Ad

Removing The 1st Element Then 1st And 2nd Element From A List Of Lists

- 1 answer

My aim is to remove the 1st element of a list and then the first and second element of a list etc. So a list entered like this: [[1,1],[1,2],[1,3],[2,1],[2,2],[2,3],[3,1],[3,2],[3,3]] will return a list like this: [[1,2],[1,3],[2,3] I have tried many things but none work well

Ad

Answer

How about something like this :

list1 = [[1,1],[1,2],[1,3],[2,1],[2,2],[2,3],[3,1],[3,2],[3,3]]
list2 = []

keep_index = 1
increment = 0
for i in range(len(list1)):
    if i == keep_index:
        list2.append(list1[i])
        increment += 2 if keep_index == 2 else 1
        keep_index += increment
print(list2)

And as @schwobaseggl commented, this code could be what you are looking for :

import itertools
itertools.combinations([1, 2, 3], 2) # combinations of 2 elements (without repeats)
Ad
source: stackoverflow.com
Ad