Hi, On 27 July 2013 11:36, Steven D'Aprano <st...@pearwood.info> wrote: > However, this will test your knowledge: > > L = [] > L.append(L) > [L] == L > > True or false? Can you explain why?
On 27 July 2013 20:14, Don Jennings <dfjenni...@gmail.com> wrote: > In [20]: bool(L) > Out[20]: True > > An empty list does evaluate to False, but a list which contains another > list (even if the contained list happens to be empty) evaluates to True. > > True but besides the point for an expression where you're comparing 2 lists -- Lists are considered equal when corresponding values are equal, not if their truthyness is the same as the truthyness of the list on the other side of the expression. In the case above, you have 2 lists. On the right hand side of the boolean expression you have L, which is a plain list, and which (crucially) contains one element, a reference to a list L. On the *left* hand side, you have another list, which also contains 1 element, a reference to list L. So, since lists are considered equal when the *values* of their corresponding elements are the same, and since each list contains 1 element, being a reference to the object (list) L, it follows that the expression [L] and L would be considered equal by Python's comparison rules, which means the expression evaluates to True. Analogously, consider the following: l1=[1] l2=l1 l3=[l1] l4=[l2] l3==l4 # True or false? Why? Walter
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor