Only New Items

Python for AIHard+10 XP+10

Only New Items

A shared shopping list lives in a file, one item per line. People keep adding things, and nobody wants duplicates.

Write add_new_items(path, items):

  1. Read the items already in the file. Strip each line.
  2. Append each item from items that is not already on the list, one per line, each followed by \n.
  3. If the same new item appears more than once in items, add it only once.
  4. Read the file back and return all the items in it as a list (stripped), in file order.
  • Never remove or rewrite what's already in the file.
  • The file always exists, but it may be empty. It never has blank lines.
  • Capitals matter: Milk and milk are different items.

Leave the make_file helper as it is.

Examples

path = make_file('shop.txt', 'milk\n')
items = ['eggs', 'milk']
returns ['milk', 'eggs']
path = make_file("an_a.txt", "bread\njam\n")
items = ["tea", "jam", "honey"]
returns ['bread', 'jam', 'tea', 'honey']
path = make_file("an_b.txt", "")
items = ["oats", "oats", "figs"]
returns ['oats', 'figs']

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run