from itertools import groupby
from operator import itemgetter
X2 = [list(group) for key, group in groupby(X1, itemgetter(1))]
Pass a key
function to groupby
that fetches the second item of each tuple, so groupby
groups the tuples by their second items.