lists, performance..

Michael Hudson mwh at python.net
Thu Oct 31 12:44:03 EST 2002


gabor <gabor at realtime.sk> writes:

> hi,
> 
> i'm working on a graphic application ( python + opengl)..
> 
> and i have some lists of vertices... but that's not important..
> 
> let's say a have loooooooong ( long = 500 to 5000 ) lists of relatively

That's not loooooooong.  That's only just long, in my book.

> simple objects..
> 
> 2 questions:
> 
> 1. is a list implemented as a vector? i mean looking up the n-th element
> is done in o[1] (constant time)?
> 
> 2. i want to insert several lists into a big list.. better to say i want
> to concatenate them... but i don't really like the 'extent' method,
> because i want to do a deep-copy... for example:
> list1 = [ ['a','b','c'], [1,2,3,4] ,[5,6,7]]
> list2 = []
> list2.extend(list1)
> but now if i modify an element in list1, list2 gets modified too..
> for now i'm doing:
> 
> list1 = ...
> list2 = ...
> 
> for item in list2:
> 	list1.append(item)
> 
> but maybe this is too slow.. or isn't? ideally i'd like to have
> something like reserve in c++....
> so let's say i have a list which len() is 30. now i want to insert
> 20elements... wouldn't it be faster to somehow resize the list directly
> to 50, and then add the elements?

Use slices:

>>> a = range(10)
>>> a[10:] = range(10)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

> i'm worried about how many times would the list resize itself to be able
> to contain the additional 20elements if i would add them one-by-one

Standard advice here: don't worry about it now, write your program and
if you find it goes too slowly, come back and speed up the bits that
will make the most difference.

Cheers,
M.

-- 
  The Internet is full.  Go away.
                      -- http://www.disobey.com/devilshat/ds011101.htm



More information about the Python-list mailing list