Oh cool, didn't realize that you could insert lists at a specific index using
slices.
On Friday, March 1, 2013 11:33:18 AM UTC-8, Justin Israel wrote:
> +1 to the explanation by Chad.
>
>
>
> I also not too long ago realized the cool stuff you can do with slices, to
> overwrite and insert another slice into a list
>
>
>
> In [1]: aList = range(5)
>
>
>
> In [2]: print aList
>
> [0, 1, 2, 3, 4]
>
>
>
> In [3]: aList[3:] = ['a','b','c'] # insert, overwrite, and extend
>
>
>
> In [4]: print aList
>
> [0, 1, 2, 'a', 'b', 'c']
>
>
>
> In [5]: aList[-2:] = ['x','y','z'] # insert via a negative index, overwrite,
> and extend
>
>
>
> In [6]: print aList
>
> [0, 1, 2, 'a', 'x', 'y', 'z']
>
>
>
> # You can also just simply insert to a specific index
>
> In [16]: aList[3:3] = ['foo','bar','biz']
>
>
>
> In [17]: print aList
>
> [0, 1, 2, 'foo', 'bar', 'biz', 3, 4] # surrounding content preserved
>
>
>
> Pretty neat stuff for treating a list like a buffer, where you aren't
> inserting anything bigger than the list, but just replacing sections of it.
> It is almost like the bytearray, but bytearray acts like a string instead of
> a list:
>
>
>
> b = bytearray('foo bar biz')
>
> print b[4:7]
>
> # bytearray(b'bar')
>
> b[4:7] = 'REPLACE'
>
> print b
>
> # bytearray(b'foo REPLACE biz')
>
>
>
>
>
> On Mar 2, 2013, at 6:56 AM, Jesse Capper wrote:
>
>
>
> > On Friday, March 1, 2013 8:33:02 AM UTC-8, Panupat Chongstitwattana wrote:
>
> >> for a in arrays:
>
> >> and
>
> >> for a in arrays[:]:
>
> >> seem to do exactly the same thing. Is there any difference made by the [:]
> >> ?
>
> >
>
> > Use array[:] when you want a copy of the list. It has the same result as
> > doing list(array).
>
> >
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "Python Programming for Autodesk Maya" group.
>
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to [email protected].
>
> > To post to this group, send email to [email protected].
>
> > For more options, visit https://groups.google.com/groups/opt_out.
>
> >
>
> >
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.