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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:43:28+00:00 2026-06-08T07:43:28+00:00

I was wondering how to sort like values in a list, and then break

  • 0

I was wondering how to sort like values in a list, and then break like values into a sub-list.

For example: I would want a function that probably does something like

def sort_by_like_values(list):
    #python magic

>>>list=[2,2,3,4,4,10]
>>>[[2,2],[3],[4,4],[10]]
OR
>>>[2,2],[3],[4,4],[10]

I read up on the sorted api and it works well for sorting things within their own list, but doesn’t break lists up into sub-lists. What module would help me out here?

  • 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-08T07:43:30+00:00Added an answer on June 8, 2026 at 7:43 am

    Use groupby from the itertools module.

    from itertools import groupby
    
    L = [2, 2, 3, 4, 4, 10]
    
    L.sort()
    for key, iterator in groupby(L):
        print key, list(iterator)
    

    Result:

    2 [2, 2]
    3 [3]
    4 [4, 4]
    10 [10]
    

    A couple of things to be aware of: groupby needs the data it works on to be sorted by the same key you wish to group by, or it won’t work. Also, the iterator needs to be consumed before continuing to the next group, so make sure you store list(iterator) to another list or something. One-liner giving you the result you want:

    >>> [list(it) for key, it in groupby(sorted(L))]
    [[2, 2], [3], [4, 4], [10]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays of values that I would like to combine - but
I want to use the pack() function in Perl to encode some data. Then
Hi I am using the following insertion sort algorithm and I would like to
I am wondering how you would approach this problem I have two Taxrates that
I have a SQL varchar column that has values like 100, 2000 and S5000.
I was wondering if there are any sweet languages that offer some sort of
This question is probably for MySQL experts and admins that have done this sort
Essentially I want to take a list of things like... a 345 b 762
I have a List(Of MyObject) that I need to sort. So I've made sure
Hey I was just wondering if there is a cool one liner that would

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.