Home > Mobile >  Iterate through sequence of values to use for a function's arguments
Iterate through sequence of values to use for a function's arguments

Time:07-19

I have the below function that returns a query that I would then use for my API call:

def creator_rating_query(self, creator_ids, platform, ages, gender):
    data = [
        {
            "query": {
                "included": {
                    "creators": creator_ids
                }
            },
            "filter": {
                "audience": {
                    "genders": gender,
                    "ages": ages
                },
                "period": {
                    "since": "2021-01",
                    "until": "2021-01"
                },
                "platform": platform
            }
        }
    ]
    return data

creator_ids are a fixed list of 200 string values, for ex:

creators_id = ['cdkfsd1','kdsfdd','dskfdfie']

however platform, ages and gender needs to be looped through for each value. I placed them in a list of a list but if there is a better way to have them arranged I'm open to suggestions!

    gender=[['f'],['m']]
    platform=[['Facebook'],['YouTube']]
    ages=[['13_17'],['18_24'],['25_34'],['35_44'],['45_54'],['55_plus']]

so the first query data returned would get each gender, each platform and each age for ex:

[
        {
            "query": {
                "included": {
                    "creators": ['cdkfsd1','kdsfdd','dskfdfie']
                }
            },
            "filter": {
                "audience": {
                    "genders": "f",
                    "ages": ['13_17']
                },
                "period": {
                    "since": "2021-01",
                    "until": "2021-01"
                },
                "platform":"Facebook"
            }
        }
    ]

and another would be:

[
        {
            "query": {
                "included": {
                    "creators": ['cdkfsd1','kdsfdd','dskfdfie']
                }
            },
            "filter": {
                "audience": {
                    "genders": "f",
                    "ages": ['18_24']
                },
                "period": {
                    "since": "2021-01",
                    "until": "2021-01"
                },
                "platform":"Facebook"
            }
        }
    ]

etc, so the same would be built for all ages, platforms, and genders

CodePudding user response:

Probably you want to find product of three lists:

from itertools import product

result = []
for gender, platform, age in product(gender, platform, ages):
    result.append({
        "query": {
            "included": {
                "creators": ['cdkfsd1', 'kdsfdd', 'dskfdfie']
            }
        },
        "filter": {
            "audience": {
                "genders": gender,
                "ages": age
            },
            "period": {
                "since": "2021-01",
                "until": "2021-01"
            },
            "platform": platform
        }
    })
print(result)

Or you can explicitly iterate over lists:

for age in ages:
    for platform in platforms:
        for gender in genders:
            print(age, platform, gender)
  • Related