Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7802205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:11:32+00:00 2026-06-02T01:11:32+00:00

While working on a problem from Google Python class, I formulated following result by

  • 0

While working on a problem from Google Python class, I formulated following result by using 2-3 examples from Stack overflow-

def sort_last(tuples):
    return [b for a,b in sorted((tup[1], tup) for tup in tuples)]

print sort_last([(1, 3), (3, 2), (2, 1)])

I learned List comprehension yesterday, so know a little about list comprehension but I am confused how this solution is working overall. Please help me to understand this (2nd line in function).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:11:35+00:00Added an answer on June 2, 2026 at 1:11 am

    That pattern is called decorate-sort-undecorate.

    1. You turn each (1, 3) into (3, (1, 3)), wrapping each tuple in a new tuple, with the item you want to sort by first.
    2. You sort, with the outer tuple ensuring that the second item in the original tuple is sorted on first.
    3. You go back from (3, (1, 3)) to (1, 3) while maintaining the order of the list.

    In Python, explicitly decorating is almost always unnecessary. Instead, use the key argument of sorted:

    sorted(list_of_tuples, key=lambda tup: tup[1]) # or key=operator.itemgetter(1)
    

    Or, if you want to sort on the reversed version of the tuple, no matter its length:

    sorted(list_of_tuples, key=lambda tup: tup[::-1]) 
                                  # or key=operator.itemgetter(slice(None, None, -1))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

while working on a school project i ran into a problem using javascript to
Im following this article to get a geojson file from an excel using Google
Many years ago while working on a tight graphics I/O problem, Tom Duff unrolled
I have been working on this problem for a while now. I am trying
i've been working on a problem for a while now, which involves targeting the
While working on an existing project I suddenly got the following error when trying
A friend of mine and I have been using Google Docs for a while
I've got the following code in my headers to load a font from Google's
I'm working through 'Dive Into Python' on Google App Engine and came across this
I've encountered a very annoying problem while working with Firefox MSAA (). I tried

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.