[Tutor] Function calls

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Nov 15 19:21:10 CET 2004



On Sun, 14 Nov 2004, Alan Gauld wrote:

> > >>> min3(1,3,4,5,6,[1,3,4,0])
> > 1
> > >>> min3(1,2,3,4,5,(1,0))
> > 1
> >
> >
> > Why isnt it showing '0' as the minimun value. this
> > function should report min value irrespective of a
> > list or a tuple passed to an argument as a call.
>
> In both cases there are only 6 elements to be sorted,
> sort does not unpack nested lists/tuples.


Yes, Python does not do any list flattening by itself: if you need it, you
need to write it explicitely.  This is a point that may be slightly
confusing to people coming from a Perl background, so maybe we should show
it:

###
>>> stuff = [1, 2, [3, 4]]
>>> stuff[0]
1
>>> stuff[1]
2
>>> stuff[2]
[3, 4]
>>> list(stuff)
[1, 2, [3, 4]]
###


If we want a list to get flattened, we can look at:

    http://aspn.activestate.com/ASPN/Mail/Message/python-list/453883


Hope this helps!



More information about the Tutor mailing list