One Item Per Line

Python for AIEasy+10 XP+10

One Item Per Line

A shopping list file should have one item on each line.

Write save_list(path, items). It writes every item in the list items into the file called path, each followed by a newline (\n). Then it reads the file back and returns the text.

  • Every line ends with \n, including the last one.
  • An empty list gives an empty file.

Use with for both writing and reading.

Examples

path = 'shop.txt'
items = ['milk', 'eggs']
returns 'milk\neggs\n'
path = "sl_a.txt"
items = ["bread", "jam", "tea"]
returns 'bread\njam\ntea\n'
path = "sl_b.txt"
items = ["solo"]
returns 'solo\n'

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