For a readable file with one value per line, convert each value to text and write it with a context manager. For a list or nested list you want to reload with its structure intact, use JSON. Pickle can preserve more complex Python objects, but never load a pickle file from an untrusted source.
Write values to a plain-text file
A text file stores characters, so convert each value to a string representation. This example writes one value per line:
values = [10, 20, 30]
with open("array.txt", "w", encoding="utf-8") as f:
f.writelines(f"{value}n" for value in values)
The context manager closes the file when the block ends, including if an exception occurs. The "w" mode creates the file or replaces its existing contents; encoding="utf-8" makes the text encoding explicit.
This format is easy to inspect, but it does not record the original types or structure. To read it back, your program must know that each line represents one value and convert the text as needed. Python’s tutorial documents that f.write(string) writes a string and returns the number of characters written: Python Tutorial: Reading and Writing Files.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minute#1 Best Overall
Save a list or nested list as JSON
Use JSON when you want list structure to survive saving and reloading, or when another application may need to read the data. Python’s json module is part of the standard library:
import json
values = [[1, 2], [3, 4]]
with open("array.json", "w", encoding="utf-8") as f:
json.dump(values, f)
with open("array.json", encoding="utf-8") as f:
restored = json.load(f)
Here, restored is a Python list with the nested structure. JSON supports common data types such as lists and dictionaries, but it does not automatically serialize every Python object or custom class; those require an appropriate conversion. Python’s tutorial recommends UTF-8 when opening JSON files: Python Tutorial: Saving Structured Data with JSON.
Rank #2
A JSON file is not a sequence of independent documents. Calling json.dump() repeatedly on the same file does not create valid separate JSON values; instead, write one enclosing value or choose a record format designed for multiple records. See the Python JSON library reference.
Choose a file format for your goal
| Goal | Starting format | Trade-off |
|---|---|---|
| Inspect values easily | Plain text, often one value per line | You must define how to parse the lines and restore types. |
| Keep a list or nested data structured, including for other software | JSON | Values must be JSON-compatible or converted first. |
| Restore more complex Python objects | Pickle | Python-specific, and unsafe to load from an untrusted source. |
When pickle is appropriate—and when it is not
Pickle can serialize Python-specific objects that do not fit naturally into JSON. It is not a suitable interchange format for applications written in other languages. More importantly, deserializing an untrusted pickle file can execute arbitrary code. Only load pickle files from sources you trust. Python explains these limitations in its tutorial section on structured data and pickle.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →What “array” means in this example
Python programmers may use “array” to mean an ordinary list, the standard-library array type, or a NumPy ndarray. The examples here show ordinary Python lists. NumPy arrays may call for NumPy-specific I/O methods, so do not assume these examples cover their storage requirements.
Quick Recap
Best Value
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.




