[Tutor] List problem

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Thu, 3 May 2001 02:00:20 -0700 (PDT)


On Thu, 3 May 2001, Sharriff Aina wrote:

> I tried it ou, heres one variation:
> ### code 
> 
> if allrows[0] == []:


There's a large difference between:

    if allrows[0] == []

and

    if allrows == []

It's only three characters, but to Python, it makes a world of difference.  
Here's an example that might make things clearer:

###
>>> l1 = [()]   # a list whose first element is an empty tuple
>>> l2 = []     # the empty list.
>>> l1[0] == []
0
>>> l2[0] == []
Traceback (innermost last):
  File "<stdin>", line 1, in ?
IndexError: list index out of range
>>> l1 == []
0
>>> l2 == []
1       
###

Let's make a somewhat silly analogy... hmm...

    [()]:  "a honey jar that contains a deflated balloon."
    []  :  "the empty honey jar."

We need to check that allrows isn't an empty honey jar; otherwise, our
hands would get sticky for no good reason.


(Could you make sure you're selecting the "reply to all" option when you
respond to our email?  It's usually better to do this because the other
tutors can load-balance.  Thanks!)