Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python – iterate through sequence of values to use for a function's arguments

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading