-3

I am currently learning python. I would like to know how can I compare two list. I have two lists:

days = ['sunday', 'monday', 'tuesday', 'wednesday', 'friday']
new_days = ['weekend', 'wednesday', 'saturday', 'sunday', 'monday','thursday']

How can I write, I'm assuming a loop, a code to see if all the elements from the days list are in the new_days list and then if there are any missing elements from the days list that aren't in the new_days list print out how many are missing and print out the ones that are missing? Would I use the all() function to compare the two to begin with?

1 Answers1

0

if you are just interested in finding the difference between both list you could use the following code:

days = ['sunday', 'monday', 'tuesday', 'wednesday', 'friday']
new_days = ['weekend', 'wednesday', 'saturday', 'sunday', 'monday','thursday']
difference = list(set(days) - set(new_days))

how this helps

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
  • This was very helpful! So using the set for both lists and then converting them into list to find the different. Thank you so much! But how can I loop this to check each item and then when finishing checking all items print? – Abdul-Aziz Muhammad Apr 09 '19 at 21:54
  • @Abdul-AzizMuhammad I'm happy that this helped! if you could click on the grey tick near the answer to validate it that would be great! – Nazim Kerimbekov Apr 09 '19 at 21:56
  • My only question is why use list if they are already in list format? – Abdul-Aziz Muhammad Apr 10 '19 at 17:23
  • We use list because we have convert both variables into sets using set(). In order to get the final result as a list we have to use list(). – Nazim Kerimbekov Apr 10 '19 at 17:24