On Mon, Jun 30, 2008 at 4:47 PM, Dick Moores <[EMAIL PROTECTED]> wrote:
> Show me a better way?

You can do it with slice assignment too:
>>> a = [1, 2, 3, 4]
>>> a[1:3] = [[7, 8]]
>>> a
[1, [7, 8], 4]

Now, which way is better?  The answer depends on context.  The best
way to write it is in the manner that it makes the most sense to
someone reading your program (including you, several years later)!

If, conceptually, you are removing two elements from a list, then
adding a new element which is itself a list, then doing it with remove
and insert is probably best.  On the other hand, if you are picking
two elements out of the list (a slice) and replacing it with a new
sublist, then the slice assignment may make more sense to the reader.

-- 
Jerry
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to