title: Dynamically change range in Python?
body: <p>So say I'm using BeautifulSoup to parse pages and my code figures out that there are at least 7 pages to a query.</p>
  
  <p>The pagination looks like</p>
  
  <pre><code> 1 2 3 4 5 6 7 Next
  </code></pre>
  
  <p>If I paginate all the way to 7, sometimes there are more than 7 pages, so that if I am on page 7, the pagination looks like</p>
  
  <pre><code> 1 2 3    7 8 9 10 Next
  </code></pre>
  
  <p>So now, I know there are at least 3 more pages. I am using an initial pass to figure out how many pages i.e. get_num_pages returns 7</p>
  
  <p>What I am doing is iterating over items on each page so I have something like</p>
  
  <pre><code>for page in range(1,num_pages + 1):
    # do some stuff here
  </code></pre>
  
  <p>Is there a way to dynamically update the range if the script figures out there are more than 7 pages? I guess another approach is to keep a count and as I get to page 7, handle that separately.  I'm looking for suggestions and solutions for the best way to approach this. </p>