On Wed, April 12, 2006 2:42 pm, Kaushal Shriyan wrote: >>>> list[:] --> Does this mean its list[0:0] > ['a', 'b', 'c', 'd', 'e', 'f'] ----> I didnot understood this
I remember you once asked another question related to sequence slices and I mentioned the effects of slicing with no indices. You sure you're reading the replies? ;) When you leave an index in the slice argument, it will assume that you meant the maximum (or minimum as the case may be) possible. For example. >>> foo = ['a','b','c','d','e'] >>> foo[2] #Element at index 2 'c' >>> foo[2:] #Elements from index two till the end ['c', 'd', 'e'] >>> foo[:2] #Elements from the beginning till index 2. ['a', 'b'] >>> Now, when you give a [:] to the slice operator, you get a copy of the original list. This is shown below >>> bar = foo[:] #bar contains a copy of foo >>> baz = foo #baz is an alias for foo (not a copy) >>> baz is foo #Are baz and foo the same? True #Yes they are >>> bar is foo #Are bar and foo the same? False #No they're not. It's a copy remember? >>> bar[2]="test" #Change element at index 2 of bar to "test" >>> bar # Print it ['a', 'b', 'test', 'd', 'e'] # It's changed >>> foo # Has foo changed as well? ['a', 'b', 'c', 'd', 'e'] # Nope. Because bar is a copy of foo. >>> baz[2]="test" # Now we change element at index 2 of baz. >>> baz # Has baz changed? ['a', 'b', 'test', 'd', 'e'] # Of course. :) >>> foo # Has foo changed? ['a', 'b', 'test', 'd', 'e'] # Yes. Since baz was an alias of foo. >>> I trust this clears things up. Also, try not to use "list" as a variable name since it's a builtin. -- -NI _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor