Home > Software engineering >  How to implement a for loop to iterate over this dataset
How to implement a for loop to iterate over this dataset

Time:09-20

I'm implementing the following code to get match history data from an API:

my_matches = watcher.match.matchlist_by_puuid(
        region=my_region, 
        puuid=me["puuid"], 
        count=100,
        start=1)

The max items I can display per page is 100 (count), I would like my_matches to equal the first 1000 matches, thus looping start from 1 - 10.

Is there any way to effectively do this?

CodePudding user response:

Based on the documentation (see page 17), this function returns a list of strings. The function can only return a 100 count max. Also, it accepts a start for where to start returning these matches (which defaults at 0). A possible solution for your problem would look like this:

allMatches = [] # will become a list containing 10 lists of matches
for match_page in range(9): # remember arrays start at 0!
    countNum = match_page * 100 # first will be 0, second 100, third 200 etc...
    my_matches = watcher.match.matchlist_by_puuid(
    region=my_region, 
    puuid=me["puuid"], 
    count=100,
    start=countNum)
    # ^ Notice how we use countNum as the start for returning
    allMatches.append(my_matches)

CodePudding user response:

If you want to remain concise, and you want your matchesto be a 1000 long list of results, you can concatenate direclty all the outputs of size 100 as:

import itertools
matches = list(itertools.chain.from_iterable(watcher.match.matchlist_by_puuid(
        region=my_region, 
        puuid=me["puuid"], 
        count=100,
        start=i*100) for i in range(10)))
  • Related