Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sort last
#1
I have been taking a class, so this must count as homework. So thing is given a list of non-empty tuples, return a list sorted in increasing order by the last element in each tuple. e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields [(2, 2), (1, 3), (3, 4, 5), (1, 7)] Hint: use a custom key= function to extract the last element form each tuple.
Here's my go :
def sort_last(tuples):
     for t in tuples :
          last_element = t[-1]
     return sorted(tuples, key = last)
Unfortunately, this is all i have been able to do. I don't know how can I sort the list on the basis of its last element in ascending order. Any help would be appreciated
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Messages In This Thread
Sort last - by pyzyx3qwerty - May-04-2020, 10:53 AM
RE: Sort last - by anbu23 - May-04-2020, 11:06 AM
RE: Sort last - by DeaD_EyE - May-04-2020, 11:33 AM
RE: Sort last - by pyzyx3qwerty - May-04-2020, 02:22 PM
RE: Sort last - by Yoriz - May-04-2020, 03:52 PM
RE: Sort last - by pyzyx3qwerty - May-05-2020, 10:48 AM
RE: Sort last - by pyzyx3qwerty - May-04-2020, 04:24 PM
RE: Sort last - by DeaD_EyE - May-05-2020, 02:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to sort a list without .sort() function letmecode 3 3,602 Dec-28-2020, 11:21 PM
Last Post: perfringo
  [split] Manual Sort without Sort function fulir16 2 3,322 Jun-02-2019, 06:13 AM
Last Post: perfringo
  Manual Sort without Sort function dtweaponx 26 49,859 Jun-01-2019, 06:02 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020