[Tutor] 'Last' in Python?
Mark Lawrence
breamoreboy at yahoo.co.uk
Mon Mar 11 01:38:06 CET 2013
On 11/03/2013 00:16, Dave Friedman wrote:
> Hi all,
>
> I'm teaching myself Python in part with Google's course, and one of the
> exercises is to sort a list of tuples by the last element.
> e.g.: a list [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields [(2, 2), (1, 3),
> (3, 4, 5), (1, 7)].
>
> My answer, after figuring out lambda functions, is
> def sort_last(tuples):
> return sorted(tuples, key= lambda t: t[-1])
>
> Google's answer is much more straightforward, except for one part.
> def sort_last(tuples)
> return sorted(tuples, key=last)
>
> What is 'last', and where can I find a description of it? Searching the
> web and the python docs hasn't been helpful. (Maybe I'm searching badly?)
> Insight and pointers appreciated.
>
> Thanks,
> -Dave
>
>
'last' is a user defined function that does exactly the same as your
lamdba function as shown here
https://github.com/megantaylor/Google-Python-Exercises/blob/master/list1.py
--
Cheers.
Mark Lawrence
More information about the Tutor
mailing list