Interpretation of range(n) and boolean list, one-to-one map, simpler?
Question: 
    #!/usr/bin/python
    #
    # Description: bitwise factorization and then trying to find
    # an elegant way to print numbers
    
    # Source: http://forums.xkcd.com/viewtopic.php?f=11&t=61300#p2195422
    # bug with large numbers such as 99, but main point in simplifying it
    #
    def primes(n):
        # all even numbers greater than 2 are not prime.
        s = [False]*2 + [True]*2 + [False,True]*((n-4)//2) + [False]*(n%2)
        i = 3;
        while i*i < n:
            # get rid of ** and skip even numbers.
            s[i*i : n : i*2] = [False]*(1+(n-i*i)//(i*2))
            i += 2
            # skip non-primes
            while not s[i]: i += 2
        return s
    
    
    # TRIAL: can you find a simpler way to print them?
    # feeling the overuse of assignments but cannot see a way to get it simpler
    #
    p = 49
    boolPrimes = primes(p)
    numbs = range(len(boolPrimes))
    mydict = dict(zip(numbs, boolPrimes))
    
    print([numb for numb in numbs if mydict[numb]])
    

**Something I am looking for, can you get`TRIAL` to be of the extreme
simplicity below? Any such method?**

    
    
    a=[True, False, True]
    b=[1,2,3]
    b_a                    # any such simple way to get it evaluated to [1,3]
                           # above a crude way to do it in TRIAL
    


Answer: For python2.7+, you can use
[itertools.compress](http://docs.python.org/py3k/library/itertools.html#itertools.compress)

    
    
    itertools.compress(b,a)
    

eg

    
    
    >>> from itertools import compress
    >>> a=[True, False, True]
    >>> b=[1,2,3]
    >>> list(compress(b,a))
    [1, 3]
    

otherwise you can use a list comprehension

    
    
    >>> [j for i,j in zip(a,b) if i]
    [1, 3]
    

If you want to do this on your list of primes, it may be simpler to use
enumerate

    
    
    >>> primes = [False, False, True, True, False, True]
    >>> list(compress(*zip(*enumerate(primes))))
    [2, 3, 5]
    

