Python version of code to analyse list of sessions