the important conceptual thing to note here is that mylist[:] is a shallow
*copy* of mylist

for example:

mylist = [1,2,3,4,5]
for i in mylist:
    print mylist.pop(-1)

this code prints:

5
4
3

this is because we are modifying mylist during the loop: with each
iteration it is getting shorter..

but the following will print every element:

mylist = [1,2,3,4,5]
for i in mylist[:]:
    print mylist.pop(-1)

5
4
3
2
1

so, you may see that construct used when the list being looped over is also
being modified in place.  it can also be used to simply make a copy.

l1 = [1,2,3,4,5]
l2 = l1[:]
l2.pop(-1)
print l1
print l2

[1, 2, 3, 4, 5]
[1, 2, 3, 4]

-chad






On Fri, Mar 1, 2013 at 9:11 AM, Chad Fox <[email protected]> wrote:

> The colon ':' is used to return list items above, below or in between any
> index numbers your provide. This is also known as index slicing. However
> when you don't provide any index numbers it simply returns every item in
> the list.
>
> Usage eg.
>
> >> testList = [0,1,2,3,4,5]
> >> print testList[:]  # all items
> [0,1,2,3,4,5]
>
> >> print testList[3:]  #all above and including index 3
> [3,4,5]
>
> >> print testList[:3] # all below, but not including index 3
> [0,1,2]
>
> >> print testList[2:5] # returns indices  2,3 and 4, but not 5
> [2,3,4]
>
> You can also use negative numbers which will count back from the end of
> the list.
>
> >> print testList[-2] # returns 2nd to last item in the list
> [4]
>
> >> print testList[:-2] # returns items up to, but not including the 2nd to
> last item.
> [0,1,2,3]
>
> Additionally, as Dorian pointed out, arrays is the list object itself,
> but arrays[:] is the contents of the list object.
>
> In your loop example the result is the same because both the arrays and 
> arrays[:]
> return the entire list to be looped over.
>
> For more information refer to this python documentation page. Both the
> Stings and Lists subjects have many examples and explanations.
> http://docs.python.org/2/tutorial/introduction.html#strings
>
> Cheers
>
>
> -Chad
>
>
> On Fri, Mar 1, 2013 at 8:33 AM, Panupat Chongstitwattana <
> [email protected]> wrote:
>
>> for a in arrays:
>> and
>> for a in arrays[:]:
>> seem to do exactly the same thing. Is there any difference made by the
>> [:] ?
>>
>> --
>> 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.
>
>
>

-- 
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.


Reply via email to