Search code examples
pythonlistloopsyaml

Iterating through Yaml file with many arrays (python)


I have the following yaml file:

name: Tiger
points: [100, 5000]
calls: [1, 10]

which I load into python via

with open("myfile.yaml") as f:
    myfile = yaml.safe_load(f)

What I want is to write code that automatically calls some function with every combination of values from the yaml file like this:

my_function(Tiger, 100, 1)
my_function(Tiger, 100, 10)
my_function(Tiger, 5000, 1)
my_function(Tiger, 5000, 10)

I know I could use loops like this:

for item1 in myfile["points"]:
    for item2 in myfile["calls"]:
        my_function(Tiger, item1, item2)

but my yaml file in fact has many such arrays, not just "points" and "calls" but a hundred other arrays (which may have different number of elements in the arrays, not all the same). So I want to do it in some more elegant way instead of writing a bunch of nested for statements. Is it possible?


Solution

  • You can create combinations with product function.

    Product function: Cartesian product of input iterables. Equivalent to nested for-loops.

    Sample Code:

    import yaml
    from itertools import product
    
    with open("test.yaml") as f:
        myfile = yaml.safe_load(f)
    
    arrays_to_iterate = [myfile["points"], myfile["calls"]]  
    
    # get all possible combinations
    combinations = list(product(*arrays_to_iterate))
    
    # sample function implementation here, now only prints the parameters
    def my_function(name, *args):
        print(name, *args)
        
    for combination in combinations:
        my_function(myfile["name"], *combination)
    

    YAML File (test.yaml):

    name: Tiger
    points: [100, 5000]
    calls: [1, 10]
    

    Output:

    enter image description here