Programatically concatenating an unknown number of arviz files

I need to concatenate an unknown number of InferenceData objects. At runtime, the number is known, and can be used to construct all the filenames involved. If the number would be 10, then this code would do the job, but the number is a run-time parameter. Some version of reduce came to my mind, but arviz.concat() requires the object to be of the same format so that fails. How can I generalize this code to work for any number of files?

data = arviz.concat(arviz.from_netcdf("data_0.netcdf"), arviz.from_netcdf("data_1.netcdf"),
                    arviz.from_netcdf("data_2.netcdf"), arviz.from_netcdf("data_3.netcdf"),
                    arviz.from_netcdf("data_4.netcdf"), arviz.from_netcdf("data_5.netcdf"), 
                    arviz.from_netcdf("data_6.netcdf"), arviz.from_netcdf("data_7.netcdf"), 
                    arviz.from_netcdf("data_8.netcdf"), arviz.from_netcdf("data_9.netcdf"), 
                    dim = "draw")

If you store the number of files to load as n you should be able to do the following:

data = arviz.concat(
    *[arviz.from_netcdf(f"data_{i}.netcdf") for i in range(n)],
    dim = "draw"
1 Like