Array-based filtering

from array import array from random import randint, randrange from sys import getsizeof size = 1000 A_list = [randrange(-10,10) for i in range(size)] A = array( 'i', A_list) mask = array( 'H', [randint(0,1) for i in range(size)]) print(getsizeof(A_list)) # List: 9032 bytes print(getsizeof(A)) # Array: 4056 bytes print(getsizeof(mask)) # Mask: 2056 bytes A = array('i', [A[i] for i, m in enumerate(mask) if m]) print(getsizeof(A)) # Array after mask applied: 4056 -> 2028 bytes print(len(A)) # 493 elements print(A[5:10]) # array('i', [-5, 4, 5, 6, 7]) print(getsizeof(A[5:10])) # Array slice: 76 bytes print(getsizeof(A_list[5:10])) # List slice: 112 bytes