array manipulation without for loops

Alex Martelli aleax at
Sun Jun 25 18:38:10 CEST 2006

Sheldon <shejo284 at> wrote:

> I have two arrays that are of the same dimension but having 3 different
> values: 255, 1 or 2.
> I would like to set all the positions in both arrays having 255 to be
> equal, i.e., where one array has 255, I set the same elements in the
> other array to 255 and visa versa. Does anyone know how to do this
> without using for loops?

Python's Numeric extension package (still available, but not actively
developed any more) and its successors (numarray, and the even newer
numpy) are replete with such functionality -- indeed, to really use
arrays in Python you should get any one of these packages (Python offers
arrays only in the very limited incarnation of the standard library
module named array -- better than nothing, but little built-in
functionality, while those packages have plenty).

Of course, if you say "array" when actually you mean "list", the
situation is completely different (it may be worth turning list into
Numeric arrays for manipulation, then back when you're done, if speed is
absolutely of the essence and you desperately need lists as inputs and
outputs but a lot of manipulation in-between).


More information about the Python-list mailing list