Teaching : Python, Scheme, Java...

Andrew Bennetts andrew-pythonlist at puzzling.org
Thu Apr 17 04:13:44 EDT 2003


On Thu, Apr 17, 2003 at 09:55:46AM +0200, Jean-Paul Roy wrote:
[..]
> Aside from that points, a personal question as a Schemer who reads the 
> Python primer:
> - how do you ask if a list is empty ? Is the following ok ?
>       if L == [] :

Usually you'd just do
    if not L:

You could also do
    if len(L) == 0:

> - how do you ask if the list has only one element, with time 0(1) ? The
>   Scheme/Lisp analog is (null? (cdr L))

len(L) == 1

> - more generally, is the length of a list a builtin ? Is the L[k] access
>   time O(1) with a list ? Probably not. And with a tuple ?

Python lists are like C++ and Java vectors, they're stored contiguously in
memory, so indexing is O(1).  Same with tuples.  

-Andrew.






More information about the Python-list mailing list