1

I have a list of lists like the below:

lst = [['A', 2001, ...], ['A', 2001, ...], ['B', 2001, ...], ['A', 2002, ...], ['C', 2002, ...], ...]

I would like to iterate over the combinations of a zipped list between the first two elements of each inner list, excluding duplicates.

I could do the below, but it doesn't exclude duplicates.

letters = [item[0] for item in lst]
years = [item[1] for item in lst]

for letter, year in zipped(letters, years):

In this example, the pairs that I am looking to iterate over would be:

'A': 2001
'B': 2001
'A': 2002
'C': 2002

(Note the exclusion of the additional 'A':2001 pair)

I feel that my attempted code is more complicated than it needs to be. ANy ideas are much appreciated.

maxipod
  • 79
  • 6

2 Answers2

1

Use a set to remove the duplicates:

for letter, year in set(zipped(letters, years)):
   ...
Learning is a mess
  • 7,479
  • 7
  • 35
  • 71
0

Use groupby and ignore the subiterator.

from itertools import groupby


def f(x):
    return x[:2]


for (letter, number), _ in groupby(sorted(lst, key=f), f):
    print(letter, number)
chepner
  • 497,756
  • 71
  • 530
  • 681