0

I'm a new Python user struggling with this task. I have Python code that generates 2 lists in the following format:

list1 = [{
        "City": "Paris",
        "Food": "Croissant",
        "Price": 10
        },
        {
        "City": "Prague",
        "Food": "Sausage",
        "Price": 15
        }]

list2 = [{
        "City": "Paris",
        "Food": "Croissant",
        "Price": 10
        },
        {
        "City": "Prague",
        "Food": "Sausage",
        "Price": 15
        },
        {
        "City": "Berlin",
        "Food": "Beer",
        "Price": 5
        }]

I would like to compare list2 to list1 to identify the differences and then spit out a new list containing just the new items. In this example, the new final list would just be:

list3 = [{
        "City": "Berlin",
        "Food": "Beer",
        "Price": 5
        }]

I tried using sets but that doesn't seem to work on these lists.

Markus
  • 1
  • 1
    `[item for item in list2 if item not in list1]` . Here list 2 is the longer list. I am pretty sure this is duplicate. you can use `[x for x in list1 if x not in list2] + [y for y in list2 if y not in list1]` here, you don't have to worry which is the longer list. – moys Feb 13 '20 at 08:06
  • 1
    Does this answer your question? [Getting the difference (delta) between two lists of dictionaries](https://stackoverflow.com/questions/19755376/getting-the-difference-delta-between-two-lists-of-dictionaries) – funie200 Feb 13 '20 at 08:06
  • I would avoid dictionaries for this use-case, maybe a `namedtuple` or a dataclass – juanpa.arrivillaga Feb 13 '20 at 08:43

0 Answers0