Slicing Arrays in this way

John Machin sjmachin at lexicon.net
Wed May 2 20:44:41 EDT 2007


On May 3, 10:21 am, Michael Hoffman <cam.ac... at mh391.invalid> wrote:
> Tobiah wrote:
>
> >  >>> elegant_solution([1,2,3,4,5,6,7,8,9,10])
> > [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
>
> That's not an array, it's a list. See the array module for arrays
> (fixed-length, unlike variable-length lists).

You must have your very own definitions of "fixed-length" and
"unlike".

>>> import array
>>> fixed = array.array('b')
>>> fixed.append(42)
>>> fixed.extend([0, 1, 127])
>>> fixed
array('b', [42, 0, 1, 127])
>>> fixed.append(2)
>>> fixed
array('b', [42, 0, 1, 127, 2])
>>> fixed[2:4] = array.array('b', [8])
>>> fixed
array('b', [42, 0, 8, 2])
>>>





More information about the Python-list mailing list