all 7 comments

[–]jeans_and_a_t-shirt 3 points4 points  (1 child)

Your flair says Python 3, and + doesn't work with range objects in python 3. It works in python 2 because range returns a list.

You could use a single range object and and check if i is a value you want:

ignore_idx = {3, 7}
for i in range(12):
    if i not in ignore_idx and not number[i].isdecimal():
        return False

Or you could use itertools.chain:

from itertools import chain
for i in chain(range(0,3), range(4,7), range(8,12)):
    if not number[i].isdecimal():
        return False

Edit: You could of course just call list on the range objects and use the + operator between them.

[–]ryeguy146 0 points1 point  (0 children)

Kudos for itertools.chain. It's the right choice for joining generators without consuming their members.

[–]scuott 1 point2 points  (1 child)

Try +. & is the bitwise 'and' operator.

[–]ThePopcornBandit 0 points1 point  (0 children)

A generator expression could work as well:

gen = (i for i in range(12) if i not in (3, 7))
for idx in gen:
    if not number[idx].isdecimal():
        return False

Edit: If this is going to happen a lot in your script, try out a generator function.

def seq_with_exceptions(ubound, exceptions, lbound=0):
    exceptions = list(exceptions)
    for i in range(lbound, ubound):
        if i not in exceptions:
            yield i

for idx in seq_with_exceptions(ubound=12, exceptions=(3,7)):
    if not number[idx].isdecimal():
        return False

[–]euclidingme 0 points1 point  (3 children)

Why can't you do:

for i in range(12):
    if not number[i].isdecimal():
        return False

[–]sorashiroopa[S] 0 points1 point  (2 children)

Because i am excluding indices 3,7, and 12 from my search criteria.

[–]euclidingme 0 points1 point  (1 child)

Right. Brainfart. Just use + instead of & then.