[Tutor] Puzzled by print lst.sort()

Liam Clarke ml.cyresse at gmail.com
Sun Oct 1 15:15:47 CEST 2006


Dick Moores wrote:
> At 01:37 PM 9/30/2006, Shantanoo Mahajan wrote:
>   
>> Maybe following is helpful:
>>
>>     
>>>>> a=[3,2,1]
>>>>> b=a[:]
>>>>> b.sort()
>>>>> c=sorted(a)
>>>>> print a,b,c
>>>>> [3, 2, 1] [1, 2, 3] [1, 2, 3]
>>>>>
>>>>>           
>> Shantanoo
>>     
>
> Sorry to be dense, but I don't see what showing what happens to a 
> copy of list a adds to the explanation:
>  >>> a = [3,2,1]
>  >>> b = a[:]
>  >>> b.sort()
>  >>> b
> [1, 2, 3]
>  >>>
>
> given that:
>
>  >>> a = [3,2,1]
>  >>> a.sort()
>  >>> a
> [1, 2, 3]
>  >>>
>
> I suppose I've misunderstood how you were trying to assist me.
>
> Thanks,
>
> Dick
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>   
I think what Shantanoo was getting at was the reference thing, i.e.,

 >>> a = [3,2,1]
 >>> b = a[:]
 >>> b.sort()
 >>> a
[3, 2, 1]
 >>> b
[1, 2, 3]
 >>>

As opposed to

 >>> a = [3,2,1]
 >>> b = a
 >>> b.sort()
 >>> a
[1, 2, 3]
 >>> b
[1, 2, 3]
 >>>

You may not always have access to Python 2.4.

Regards,

Liam Clarke


More information about the Tutor mailing list