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 6842893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:09:27+00:00 2026-05-27T00:09:27+00:00

input: [‘abc’, ‘cab’, ‘cafe’, ‘face’, ‘goo’] output: [[‘abc’, ‘cab’], [‘cafe’, ‘face’], [‘goo’]] The problem

  • 0
input: ['abc', 'cab', 'cafe', 'face', 'goo']
output: [['abc', 'cab'], ['cafe', 'face'], ['goo']]

The problem is simple: it groups by anagrams. The order doesn’t matter.

Of course, I can do this by C++ (that’s my mother tongue). But, I’m wondering this can be done in a single line by Python. EDITED: If it’s not possible, maybe 2 or 3 lines. I’m a newbie in Python.

To check whether two strings are anagram, I used sorting.

>>> input = ['abc', 'cab', 'cafe', 'face', 'goo']
>>> input2 = [''.join(sorted(x)) for x in input]
>>> input2
['abc', 'abc', 'acef', 'acef', 'goo']

I think it may be doable by combining map or so. But, I need to use a dict as a hash table. I don’t know yet whether this is doable in a single line. Any hints would be appreicated!

  • 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-27T00:09:27+00:00Added an answer on May 27, 2026 at 12:09 am

    A readable one-line solution:

    output = [list(group) for key,group in groupby(sorted(words,key=sorted),sorted)]
    

    For example:

    >>> words = ['abc', 'cab', 'cafe', 'goo', 'face']
    >>> from itertools import groupby
    >>> [list(group) for key,group in groupby(sorted(words,key=sorted),sorted)]
    [['abc', 'cab'], ['cafe', 'face'], ['goo']]
    

    The key thing here is to use itertools.groupby from the itertools module which will group items in a list together.

    The list we supply to groupby has to be sorted in advanced so we pass it sorted(words,key=sorted). The trick here is that sorted can take a key function and will sort based on the output from this function, so we pass sorted again as the key function and this will will sort the words using the letters of the string in order. There’s no need to define our own function or create a lambda.

    groupby takes a key function which it uses to tell if items should be grouped together and again we can just pass it the built-in sorted function.

    The final thing to note is that the output is pairs of key and group objects, so we just take the grouper objects and use the list function to convert each of them to a list.

    (BTW – I wouldn’t call your variable input as then your hiding the built-in input function, although it’s probably not one you should be using.)

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

Sidebar

Related Questions

Input : {5, 13, 6, 5, 13, 7, 8, 6, 5} Output : {5,
Input: SHC 111U,SHB 22x,, SHA 5555G Needed output: SHB 22X, SHC 111U, SHA 5555G
Input: I have a LaTeX file, with plain text & math formulas. Desired output:
Input col1, col2, col3, coln Output @col1, @col2, @col3, @coln
Input: Hi. I am John. My name is John. Who are you ? Output:
input: name <hui.li@xxx.ch>; hans@dhdfhgdfgh <hans.dampf@xxxx>; Output: e1@mail.com, e2@mail.com, e3@mail.com,e@mail.com I want to erase the
Input String: 115.0000 Output String should be like: 115.00 i used this code: String.format(%.2f,115.0000);
Input : [1,2,2,3,4,2] Output : Index of 2 = [1,2,5]
Input: X(P)~AK,X(MV)~AK Expected Output: P(curr=AK),MV(curr=AK) Using C#3.0 I solved by using string functions(split,then appending
My input file is as below : HEADER {ABC|*|DEF {GHI 0 1 0} {{Points

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.