Can You help me to convert Python list:
[(1, 'a'), (2, 'b'), (2, 'c'), (3, 'd'), (3, 'e')]
so that:
(1, 'a') is index 0
(2, 'b'), (2, 'c') are both index 1
(3, 'd'), (3, 'e') are both index 2
Simply, all tuples which element[0] is equal, have same index.
Thank You,
itertools.groupbyto the rescue!:You could use
operator.itemgetter(0)instead of thelambdaif you wanted…demo: