List of Lists in C
Hi, Quick question, I've been doing a fair bit of extension writing in C recently, but wondered how best to implement:
l = [[]] * 5
to create a list of a given length containing the initialization variable desired. A loop seems the straight forward manner, but I would have thought there was a more efficient way... Currently I just do it in Python and pass through the already initialized list as it seems perfectly efficient. Cheers, Hanni
Hi, Don't remember that you are using the same list in each element of the outer list. If you don't want this, use [[] for i in range(5)]. I don't think there is another way in C either (or too complicated). Matthieu 2009/1/26 Hanni Ali <hanni.ali@gmail.com>:
Hi,
Quick question, I've been doing a fair bit of extension writing in C recently, but wondered how best to implement:
l = [[]] * 5
to create a list of a given length containing the initialization variable desired.
A loop seems the straight forward manner, but I would have thought there was a more efficient way...
Currently I just do it in Python and pass through the already initialized list as it seems perfectly efficient.
Cheers,
Hanni
_______________________________________________ Numpydiscussion mailing list Numpydiscussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpydiscussion
 Information System Engineer, Ph.D. Website: http://matthieubrucher.developpez.com/ Blogs: http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn: http://www.linkedin.com/in/matthieubrucher
Yes fair point, but when it's a empty list and new elements are replaced with a new list instance it's fine, especially as [[]]*100000 is significantly faster than [[] for i in xrange(100000)] as I was previously doing. In fact I think that's partly answered my question [[]]*x must create a list of pointers pointing at the same list. Rather than [[] for i in xrange(100000)] which must create a list of new separate lists instances. Hence the significant difference in speed. Hanni 2009/1/26 Matthieu Brucher <matthieu.brucher@gmail.com>
Hi,
Don't remember that you are using the same list in each element of the outer list. If you don't want this, use [[] for i in range(5)]. I don't think there is another way in C either (or too complicated).
Matthieu
2009/1/26 Hanni Ali <hanni.ali@gmail.com>:
Hi,
Quick question, I've been doing a fair bit of extension writing in C recently, but wondered how best to implement:
l = [[]] * 5
to create a list of a given length containing the initialization variable desired.
A loop seems the straight forward manner, but I would have thought there was a more efficient way...
Currently I just do it in Python and pass through the already initialized list as it seems perfectly efficient.
Cheers,
Hanni
_______________________________________________ Numpydiscussion mailing list Numpydiscussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpydiscussion
 Information System Engineer, Ph.D. Website: http://matthieubrucher.developpez.com/ Blogs: http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn: http://www.linkedin.com/in/matthieubrucher _______________________________________________ Numpydiscussion mailing list Numpydiscussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpydiscussion
2009/1/26 Hanni Ali <hanni.ali@gmail.com>:
Yes fair point, but when it's a empty list and new elements are replaced with a new list instance it's fine, especially as [[]]*100000 is significantly faster than [[] for i in xrange(100000)] as I was previously doing.
In this case, why do you put a list in it in the first place ? You could put None, and it would be safer ;)
In fact I think that's partly answered my question [[]]*x must create a list of pointers pointing at the same list. Rather than [[] for i in xrange(100000)] which must create a list of new separate lists instances. Hence the significant difference in speed.
Hanni
I agree. Less memory allocations and initialization, thus more speed. Matthieu  Information System Engineer, Ph.D. Website: http://matthieubrucher.developpez.com/ Blogs: http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn: http://www.linkedin.com/in/matthieubrucher
Correct, however other areas of the application expect an empty list to be present, otherwise I would have used None. 2009/1/26 Matthieu Brucher <matthieu.brucher@gmail.com>
Yes fair point, but when it's a empty list and new elements are replaced with a new list instance it's fine, especially as [[]]*100000 is significantly faster than [[] for i in xrange(100000)] as I was
2009/1/26 Hanni Ali <hanni.ali@gmail.com>: previously
doing.
In this case, why do you put a list in it in the first place ? You could put None, and it would be safer ;)
In fact I think that's partly answered my question [[]]*x must create a list of pointers pointing at the same list. Rather than [[] for i in xrange(100000)] which must create a list of new separate lists instances. Hence the significant difference in speed.
Hanni
I agree. Less memory allocations and initialization, thus more speed.
Matthieu  Information System Engineer, Ph.D. Website: http://matthieubrucher.developpez.com/ Blogs: http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn: http://www.linkedin.com/in/matthieubrucher _______________________________________________ Numpydiscussion mailing list Numpydiscussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpydiscussion
participants (2)

Hanni Ali

Matthieu Brucher