obj2 = ojb1 = range(len(somelist))

Quinn Dunkan quinn at hurl.ugcs.caltech.edu
Mon Feb 4 17:29:37 EST 2002


On 31 Jan 2002 19:45:42 -0500, Tom Jenkins <tjenkins at devis.com> wrote:
>On Thu, 2002-01-31 at 17:28, David Bear wrote:
>> I wanted to create 2 distinct lists that were derived from range
>> function.  rather that doing
>> 
>> obj1 = range(len(somelist))
>> obj2 = range(len(somelist))
>> 
>> I wanted to do
>> 
>> obj2 = ojb1 = range(len(somelist))
>> 
>> Yet, both names point to the same object -- they're not two objects.
>> 
>> Is there some other syntax that I could use to avoid having to call
>> range(len(list)) twice and still create two objects?
>
>i didn't think this was going to work but hey the interpreter was there
><wink>
>>>> l1 = l2[:] = range(len(somelist))
>>>> print l1, l2
>[0, 1, 2, 3, 4] [0, 1, 2, 3, 4]
>>>> print id(l1), id(l2)
>135566380 135560684
>>>>

That should only work if 'l2' is already an object that supports slice
assignment.



More information about the Python-list mailing list