Re: Can't take the tail of a list without copying via List[1:]?
Steven D. Majewski (sdm7g@elvis.med.virginia.edu)
Sun, 7 Aug 1994 23:45:54 -0400
On Aug 5, 13:14, Aaron Watters wrote:
>
> Summary: Can I or why can't I take the cdr of a list without
> copying the entire top level structure of the list?
>
>
> def NeatStuff(List):
> Head,Tail = List[0],List[1:]
> if Head=1:
> GroovyStuff(Tail)
> elif Head=2:
> NastyStuff(Tail)
> else:
> NeatStuff(Tail)
>
> Unfortunately List[1:] makes a _copy_ of the top level structure of the
> rest of List, which is quite expensive for large lists if all I want
> is Common Lisp's (rest List) which gives me the rest of List without
> copying anything.
>
You could try:
def NeatStuff(List):
Head = List[0]
del List[0]
Tail = List
Which will remove the first element of the list ( after assigning
to Head ). Obviously, the "Tail = List" is redundant and is there
just to keep the names the same as in the example.
- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU>
- UVA Department of Molecular Physiology and Biological Physics