0

I would like to know how to check if anything is new in a list, like this:

list_1 = [10, 30, 25]
list_2 = get_updated_list()
new_items = get_new_items(list_2)
if new_items is not []:
    print('Something is new')

I would highly appreciate it if someone had a solution, I've been thinking on how to do this for so long.

BotNC
  • 31
  • 6

3 Answers3

2

You could use the built-in set difference.

s1 = set(list_1)
s2 = set(list_2)

diff = s1.difference(s2)
if diff:
    print("Something is new")
Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
0

If you just care about the sets of values in list_1 and list_2, without regard to their order or the number of times they occur, then you can use a simple set difference:

>>> list_1 = [10, 30, 25]
>>> list_2 = [10, 25, 45]
>>> list(set(list_2) - set(list_1))
[45]
>>> 

This example shows the values that are present in list_2 but not in list_1. To see values that were removed, you can reverse the subtraction.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
0

An easy way, just do - and convert back to list:

list(set(list2)-set(list1))
Wasif
  • 14,755
  • 3
  • 14
  • 34