Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Iterate over unique pairs in zipped list

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

Use a set to remove the duplicates:

for letter, year in set(zipped(letters, years)):
   ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading