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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:06:04+00:00 2026-06-13T00:06:04+00:00

I have data like [2, 2, 2, 2, 2, 3, 13, 113] which I

  • 0

I have data like

[2, 2, 2, 2, 2, 3, 13, 113]

which I then want to sort into separate lists by keys generated by myself. In fact I want to generate all possible lists.

Some examples:

values: [2, 2, 2, 2, 2, 3, 13, 113]
keys:   [0, 0, 1, 2, 1, 3,  3,   1]
sublists: [2, 2], [2, 2, 113], [2], [3, 13]

values: [2, 2, 2, 2, 2, 3, 13, 113]
keys:   [0, 1, 0, 0, 0, 1,  1,   0]
sublists: [2, 2, 2, 2, 113], [2, 3, 13]

values: [2, 2, 2, 2, 2, 3, 13, 113]
keys:   [2, 3, 0, 0, 4, 4,  1,   3]
sublists: [2, 2], [13], [2], [2, 113], [2, 3]

All possible keys are generated by

def generate_keys(prime_factors):
    key_size = len(prime_factors) - 1
    key_values = [str(i) for i in range(key_size)]

    return list(itertools.combinations_with_replacement(key_values, \
        len(prime_factors)))

Then I thought I could use the keys to shift the values into the sublists. That’s the part I’m stuck on. I thought itertools.groupby would be my solution but upon further investigation I see no way to use my custom lists as keys for groupby.

How do I split my big list into smaller sublists using these keys? There may even be a way to do this without using keys. Either way, I don’t know how to do it and looking at other Stack Overflow questions has eben in the ballpark but not exactly this question.

  • 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-13T00:06:05+00:00Added an answer on June 13, 2026 at 12:06 am

    This does what you want:

    def sift(keys, values):
        answer = collections.defaultdict(list)
        kvs = zip(keys, values)
        for k,v in kvs:
            answer[k].append(v)
        return [answer[k] for k in sorted(answer)]
    
    In [205]: keys = [0, 0, 1, 2, 1, 3,  3,   1]
    
    In [206]: values = [2, 2, 2, 2, 2, 3, 13, 113]
    
    In [207]: sift(keys,values)
    Out[207]: [[2, 2], [2, 2, 113], [2], [3, 13]]
    

    Explanation:

    collections.defaultdict is a handy dict-like class that lets you define what should happen in the event that a key doesn’t exist in the dictionary that you’re trying to manipulate. For example, in my code, I have answer[k].append(v). We know that append is a list function, so we know that answer[k] should be a list. However, if I was using a conventional dict and I tried to append to the value of a non-existent key, I would have gotten a KeyError as follows:

    In [212]: d = {}
    
    In [213]: d[1] = []
    
    In [214]: d
    Out[214]: {1: []}
    
    In [215]: d[1].append('one')
    
    In [216]: d[1]
    Out[216]: ['one']
    
    In [217]: d
    Out[217]: {1: ['one']}
    
    In [218]: d[2].append('two')
    ---------------------------------------------------------------------------
    KeyError                                  Traceback (most recent call last)
    /Users/USER/<ipython-input-218-cc58f739eefa> in <module>()
    ----> 1 d[2].append('two')
    
    KeyError: 2
    

    This was only made possible because I defined answer = collections.defaultdict(list). If I had defined answer = collections.defaultdict(int), I would gotten a different error – one that would tell me that int objects don’t have an append method.

    zip on the other hand takes two lists (well actually, it takes at least two iterables), lets call them list1 and list2 and returns a list of tuples in which the ith tuple contains two objects. The first is list1[i] and the second is list2[i]. If list1 and list2 are of unequal length, len(zip(list1, list2)) would be the smaller value among len(list1) and len(list2) (i.e. min(len(list1), len(list2)).

    Once I’ve zipped keys and values, I want to create a dict such that maps a value from keys to a list of values from values. This is why I used a defaultdict, so that I wouldn’t have to check for the existence of a key in it before I appended to its value. If I had used a conventional dict, I would have had to do this:

    answer = {}
    kvs = zip(keys, values)
    for k,v, in kvs:
        if k in answer:
            answer[k].append(v)
        else:
            answer[k] = [v]
    

    Now that you have a dict (or a dict-like object) that maps values from keys to lists of ints that share the same key, all you need to do is get the lists which are the values of answer in sorted order, sorted by the keys of answer. sorted(answer) gives me a list of all of answers keys in sorted order.

    Once I have this list of sorted keys, all I have to do is get their values, which are lists of ints, and put all those lists into one big list and return that big list.

    … annnnnd Done! Hope that helps

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

Sidebar

Related Questions

I have data like: <td>USERID</td> <td>NAME</td> <td>RATING</td> I want to transform it into: <userid></userid>
I have data that looks like this. And I want to find the maximum
I have data like the following: var data = [{ id: 1, date: new
I have data like below AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE Now there is a
I have data like this: Team | goal a | 5 b | 8
Say I have data like this: <option value=abc >Test - 123</option> <option value=def >Test
I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ
I have MYSQL data like this id | number 1 | 3 4 |
I have some data like this : 1 TC1 PASS 2 TC2 FAIL 3
I have some data like this: -1,2752 -1,4735 1 -0,5500 1,3287 2 -1,6293 -2,1460

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.