In-place array modification

umdehoon umdehoon at
Sun Jul 18 16:20:37 CEST 2004

Hello everybody,

Say I have an array (Python array, not Numpy) that contains characters 
representing a DNA sequence. Something like

>>> from array import array
>>> s = array('c', "ATAGCTGCT")

Now I want to calculate the reverse complement of this sequence. I could 
make a simple loop:

>>> d = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C'}
>>> s.reverse()
>>> s
array('c', 'TCGTCGATA')
>>> n = len(s)
>>> for i in range(n):
...    s[i] = d[s[i]]
>>> s
array('c', 'AGCAGCTAT')

But usually DNA sequences are very long, so the loop might take a long 
time to finish. So I thought I would use the map function instead:

>>> s = map(lambda c: d[c], s)
>>> s = array('c', s)

But then I am creating memory for s from scratch. So what I would really 
like to do is to have a function similar to map, but have it modify the 
array in place. Does such a function exist in Python?

Many thanks,

Michiel de Hoon
Human Genome Center, U Tokyo.

More information about the Python-list mailing list