Home > Software engineering >  Finding common string values in 3 lists with different lenght; If statement with 3 conditions
Finding common string values in 3 lists with different lenght; If statement with 3 conditions

Time:08-04

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.

CodePudding user response:

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))
  • Related