Here is what I ended up using:
import subprocess as sp
versioned = {}
with sp.Popen(["apt","list","--installed"], encoding="ascii",
stdout=sp.PIPE, stderr=sp.STDOUT) as pipe:
for pack in pipe.stdout:
if "/" not in pack:
print(pack)
continue
name = pack.split("/",1)[0]
name_ver = name.rsplit("-",1)
if len(name_ver) == 2 and name_ver[1].isdigit():
versioned.setdefault(name_ver[0],set()).add(name_ver[1])
print(f"found {len(versioned):,d} versioned packages")
versioned = {n:v for n,v in versioned.items() if len(v) > 1}
print(f"found {len(versioned):,d} packages with multiple versions:\n{versioned}")
which prints
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
Listing...
found 240 versioned packages
found 3 packages with multiple versions:
{'cpp': {'12', '11'}, 'g++': {'12', '11'}, 'gcc': {'12', '11'}}
apt purge ...
. OTOH, the more unused packages you have, the more possibilities for damage/conflicts. I want to keep only what I use. – sds Oct 24 '22 at 23:13gcc-10
&gcc-11
oremacs-22
&emacs-23
. – sds Oct 25 '22 at 14:29