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

  • Home
  • SEARCH
  • 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 6716461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:44:11+00:00 2026-05-26T08:44:11+00:00

In Python 2.2 (don’t ask), what’s the neatest way to sort a list and

  • 0

In Python 2.2 (don’t ask), what’s the neatest way to sort a list and remove duplicates?

I can obviously write a function that would sort() then iterate, but am wondering if there’s an idiomatic one-liner.

edit: The list is short, so efficiency is not a concern. Also, the elements are immutable.

  • 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-05-26T08:44:12+00:00Added an answer on May 26, 2026 at 8:44 am

    For old python versions, and since you’re using strings, there’s no one-liner I can think of, but a pattern would probably be this, using dictionaries:

    def sorted_uniq(your_list):
        table = {}
        for s in your_list:
            table[s] = None
        k = table.keys()
        k.sort()
        return k
    

    Adapted from an ancient ActiveState code snippet thread that Alex Martelli himself wrote several comments on: http://code.activestate.com/recipes/52560/

    A shorter way with list comprehensions:

    def sort_uniq(alist):
       d = {}
       mod_list = [d.setdefault(i,i) for i in alist if i not in d]
       mod_list.sort()
       return mod_list
    

    Aside from Steven’s neat (yet slightly unattractive) one liner, I think this heads toward the fewest lines and most idiomatic way of doing it with Python 2.2:

    Thanks to Steven Rumbalski in the comments, the 2nd version can be condensed further with python’s zip function:

    def sort_uniq(alist):
       mod_list = dict(zip(alist,alist)).keys()
       mod_list.sort()
       return mod_list
    

    If list.sort() didn’t operate by side effect, we’d have a one liner. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Python so please don't flame me if I ask something
I am new to python and don't know the best way to do this.
The Zen of Python states that there should only be one way to do
I've wondered why extend/append methods of Python don't return a reference to result list.
I want to execute a function every 60 seconds on Python but I don't
I have heard before that many Python developers don't use an IDE like Eclipse
Ok, so I'm writing some python code (I don't write python much, I'm more
I need to realize a complex if-elif-else statement in Python but I don't get
I want to use the google maps hand cursor in Python but I don't
I don't know python and I'm porting a library to C#, I've encountered the

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.