Python Forum

Full Version: [SOLVED] [loop] Exclude ranges in… range?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I need to exclude pages in a loop, but those exceptions could be several pages, so I need to find a way to specify individual pages as well as a range of pages:

numpages = doc.page_ount
#TODO How to use ranges in range, eg. [1-14,25,35-41,57]?
exclude = [1,2,3,4,5]
for index in numpages:
    if not exclude[index]:
        #do stuff
Do you know of a way to do this?

Thank you.
Something like this maybe:

my_ranges = [range(1, 10), range(25, 30), range(35, 41)]
exclude = [1, 2, 3, 4, 5]

for r in my_ranges:
    for i in r:
        if i not in exclude:
            print(i)
/regards
Thanks!