Before I optimise anything I want a profile, and the standard library already has one.
python3 -m cProfile -s cumtime script.py | head -20
python3 -m cProfile -o out.prof script.py
-s cumtime sorts by cumulative time, which puts the top-level call chains first, and head trims the report. The script’s own output goes through the same pipe, so redirect it if it gets in the way. -o out.prof saves the raw data so you can inspect it later with python3 -m pstats out.prof. The profiler adds overhead, so use the proportions and not the absolute seconds. For one small expression, timeit is the better tool.
Further reading: profile and cProfile documentation