How to close all the apps open in an Android device with Python in Appium?

My device has a small RAM capacity and when too many apps are open, it starts to slow down and disturbs the execution of my script.

So I would like to close all the apps open on my smartphone before running my automation script.
So my idea was to get the list of all apps open on the device and then kill them one by one with the command:


But I didn’t find how to get a list of the open apps on a device.

I have this question too, is there a generic way to do this on Android/iOS perhaps that is just not clearly documented. Will make test fixtures and execution a bit more reliable for me. I could make a list of apps, but that feels like a hack, would like confirmation that a hard coded list is not a unreasonable approach.