Converting a c array to python list

Scott David Daniels scott.daniels at
Fri Mar 2 03:52:41 CET 2007

zefciu wrote:
> Hi!
> I want to embed a function in my python application, that creates a
> two-dimensional array of integers and passes it as a list (preferably a
> list of lists, but that is not necessary, as the python function knows
> the dimensions of this array).  As I read the reference, I see, that I
> must first initialize a list object and then item-by-item put the values
> to the list.  Is there any faster way to do it?  And is it worth to
> implement?  The same problem is resolved in the current version by
> calling a smaller c function (that counts just one element of the array)
> many times.  Will it add much performance to the process?
> zefciu

This will allow you to provide a python "View" onto your C data.
The view can be cut into a list of views as on_way or other_way below:
     view = block.View(...)
     stride = <<one dimension>>
     one_way = [view[n: n + stride] for n in range(0, len(view), stride)]
     other_way = [view[n::stride] for n in range(stride)]
One of these will give you row-major, and the other column-major access
to the live data in the C array.  If you don't want to see your program
data as it changes, you could Create a Block and fill it.  If you need
Python 2.4 or 2.5, you'll need to figure out how to build from sources
on Windows (I assume building from sources is otherwise "easy").

--Scott David Daniels
scott.daniels at

More information about the Python-list mailing list