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
#2
>>> tup=[(1, 7), (1, 3), (3, 4, 5), (2, 2)]
>>> sorted(tup, key=lambda x:x[-1])
[(2, 2), (1, 3), (3, 4, 5), (1, 7)]
>>>
Reply
#3
You can use the itemgetter.

from operator import itemgetter

tup = [(1, 7), (1, 3), (3, 4, 5), (2, 2)]
sorted(tup, key=itemgetter(-1))
The itemgetter could also used to access keys in a dict:
data = [{"x": 10, "y": 42},{"x": 20, "y": 44},{"x": 13, "y": -1}]

# sort by x
sorted(d, key=itemgetter("x"))

# sort by x and then y
sorted(d, key=itemgetter("x", "y"))

# sort by y and then x
sorted(d, key=itemgetter("y", "x"))
How it works:

my_itemgetter = itemgetter(1)
# the itemgetter returns a callable, which takes the sequence as input
my_itemgetter([1,2,3])
Output:
2
If you call it in one line, it looks a bit strange:
itemgetter(1)([1,2,3])
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
@anbu23 and @DeaD_EyE thanks for your replies.
Here's the code if anyone needs it in the future :
def sort_last(tuples):
     for t in tuples :
          last_element = t[-1]
     return sorted(tup, key=lambda x:x[-1])
tup =[ (1, 7), (1, 3), (3, 4, 5), (2, 2)]
print(sort_last(tup))
And output :
Output:
[(2, 2), (1, 3), (3, 4, 5), (1, 7)]
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
#5
  • Lines 2 & 3 are not doing anything useful, they can be removed as line 4 does all the work.
  • On line 4, sorted should be passed tuples instead of using the global tup.

Code becomes
def sort_last(tuples):
    return sorted(tuples, key=lambda x: x[-1])


tup = [(1, 7), (1, 3), (3, 4, 5), (2, 2)]
print(sort_last(tup))
Output:
[(2, 2), (1, 3), (3, 4, 5), (1, 7)]
Reply
#6
Thanks @Yoriz. Not only do i get the same expected result, but it make my codes shorter
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
#7
(May-04-2020, 03:52 PM)Yoriz Wrote: [*]On line 4, sorted should be passed tuples instead of using the global tup.
But, if I may ask, what is this global tup you are talking about? And where can it be used?
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
#8
tup is just a name, which refers to the object [(1, 7), (1, 3), (3, 4, 5), (2, 2)].

anbu23 named it tup.
I guess he has chosen this name, because the inner elements are tuples.
Finding the right names is not always easy.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to sort a list without .sort() function letmecode 3 3,439 Dec-28-2020, 11:21 PM
Last Post: perfringo
  [split] Manual Sort without Sort function fulir16 2 3,166 Jun-02-2019, 06:13 AM
Last Post: perfringo
  Manual Sort without Sort function dtweaponx 26 48,974 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