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

Finding common string values in 3 lists with different lenght; If statement with 3 conditions

I have three long lists such as:

dates_a = [20/07/2022, 21/07/2022, 22/07/2022, ... , 02/08/2022] -> 300 string objects in total
dates_b = [18/02/2021, 05/05/2021, 22/06/2022, ... , 21/07,2022] -> 200+ string objects in total
dates_c = [01/02/2022, 01/04/2022, 01/06/2022, ... , 01/08/2022] -> 100+ string objects in total

I tried tackling this with:

    for i in range(len(dates_a), len(dates_b), len(dates_c)):
  if dates_a[i] == dates_b[i] and (dates_b[i] == dates_c[i] and dates_a[i] == dates_c[i]):
    list_of_dates.append(dates_a[i])

  else:
    print("not")

print(list_of_dates)

Is there any easier approach to this? Currently this code is also failing since list_of_dates returns an empty list.

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

>Solution :

Turn them into sets so you can take the intersection, then turn the result back into a list if needed.

list_of_dates = list(set(dates_a) & set(dates_b) & set(dates_c))
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