What you need to do is first obtain the Cartesian product then you can filter and map the resulting array.
Python
from itertools import product
numbers = [ 1, 2, 3, 4, 5 ]
letters = [ 'a', 'b', 'c', 'd', 'e' ]
cross = map(lambda x:'{0}{1}'.format(*x),filter(lambda e:e[0]%2==0,product(numbers,letters)))
print cross # ['2a', '2b', '2c', '2d', '2e', '4a', '4b', '4c', '4d', '4e']
JavaScript
// Source: http://stackoverflow.com/a/15310051/1762224
function cartesian() {
var r = [], args = arguments, max = args.length - 1;
function helper(arr, i) {
for (var j = 0, l = args[i].length; j < l; j++) {
var a = arr.slice(0).concat(args[i][j]); // Clone arr
if (i === max) { r.push(a); }
else { helper(a, i + 1); }
}
}
helper([], 0);
return r;
}
var numbers = [ 1, 2, 3, 4, 5 ];
var letters = [ 'a', 'b', 'c', 'd', 'e' ];
var cross = cartesian(numbers, letters)
. filter(function(item, index, arr) { return item[0] % 2 === 0; })
. map(function(item, index, arr) { return item.join(''); });
console.log(cross); // [ '2a', '2b', '2c', '2d', '2e', '4a', '4b', '4c', '4d', '4e' ]
More Efficient Design
As Andy suggested in the comments, you could filter the numbers
array ahead-of-time so that you do not need to create such a large product.
var cross = cartesian(
numbers.filter(function(number) {
return number % 2 === 0;
}),
letters)
.map(function(item) {
return item.join('');
});