Use plain text to put one readable item on each line, JSON to save a list’s supported types and structure for loading later, or CSV when the destination needs rows and columns. In each case, Python’s standard library can write the file; the right method depends on how you’ll use it.
Write a list to a plain-text file, one item per line
For a simple text file, open it in write mode, convert each item to a string, and add a newline after it. Explicit UTF-8 encoding and a with block make the file handling predictable. These examples follow the Python 3.14.7 documentation.
As an Amazon Associate I earn from qualifying purchases.
items = ["apples", "bananas", "cherries"]
with open("items.txt", "w", encoding="utf-8") as file:
for item in items:
file.write(f"{item}n")
file.write() accepts strings, so the f-string converts each value to its text form. You can write str(item) instead if you prefer to make that conversion explicit. This approach is readable, but it does not preserve the original Python types: for example, a number written as text is not automatically restored as a number when the file is read.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsUse writelines() for a list of strings
For string items, you can write the lines with a generator expression:
#1 Best Overall
items = ["apples", "bananas", "cherries"]
with open("items.txt", "w", encoding="utf-8") as file:
file.writelines(item + "n" for item in items)
writelines() does not add line breaks for you. Include n in each string, or the values will run together.
Save a Python list as JSON to preserve its structure
Choose JSON when you want to load the list back as data and retain supported types and nesting, rather than just store a human-readable rendering. Python’s json module writes the list with json.dump() and reads it with json.load().
Rank #2
import json
items = [1, "simple", ["nested", "values"]]
with open("items.json", "w", encoding="utf-8") as file:
json.dump(items, file, ensure_ascii=False, indent=2)
with open("items.json", encoding="utf-8") as file:
restored_items = json.load(file)
JSON supports built-in data types such as lists and dictionaries; arbitrary class instances need to be converted to supported data first. The example uses UTF-8, as required for JSON files by Python’s documentation. Write one complete JSON document per file: JSON is not a framed protocol, so calling json.dump() repeatedly on the same file does not create a valid sequence of independent JSON documents.
Write a list as CSV rows
Use the csv module when your data is tabular and needs rows and columns, such as for a spreadsheet or another CSV-aware program. Here, each inner list becomes one row:
import csv
rows = [["name", "score"], ["Ada", 98], ["Lin", 95]]
with open("scores.csv", "w", newline="", encoding="utf-8") as file:
writer = csv.writer(file)
writer.writerows(rows)
For a list of dictionaries, use csv.DictWriter, specify the field names, write a header if needed, and call writerows() for the records. Open CSV files with newline=""; the CSV module handles delimiters and quoting according to its dialect. Avoid joining fields with commas yourself, because values containing commas, quotes, or newlines need proper CSV quoting.
Quick Recap
Best Value
Choose the format that fits the next step
| Format | Best for | What it preserves |
|---|---|---|
| Plain text, one item per line | Reading or editing a simple list | Text representations only; not the original types or nested structure |
| JSON | Loading supported data back into Python or another JSON-capable program | Supported types and list/dictionary structure |
| CSV | Rows and columns for spreadsheets or tabular interchange | Tabular values and fields, not arbitrary Python object types |
Prevent common file-writing mistakes
- Non-string items in a text file: convert each item with
str()or an f-string. Choose JSON or CSV instead if you need structured data. - Missing line separators: add
nyourself when usingwritelines(). - Overwriting existing content: mode
"w"creates a file or truncates it before writing. Use"a"when you intend to append instead. - Unspecified text encoding: set
encoding="utf-8"unless your use case requires another encoding. - File not properly closed: use
with; Python closes the file when the block ends, including when an exception occurs. - CSV assembled by hand: use
csv.writerorcsv.DictWriterso delimiters, quotes, and newlines are handled as CSV.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




