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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:34:34+00:00 2026-05-16T03:34:34+00:00

I need to invert a dictionary of lists, I don’t know how to explain

  • 0

I need to invert a dictionary of lists, I don’t know how to explain it in English exactly, so here is some code that does what I want. It just takes too much memory.

def invert(oldDict):
    invertedDict = {}
    for key,valuelist in oldDict.iteritems():
        for value in valuelist:
            try:
                entry = invertedDict[value]
                if key not in entry:
                    entry.append(key)
            except KeyError:
                invertedDict[value] = [key]
    return invertedDict

The original is a dict of lists, and the result is a dict of lists. This “inverts” it.

test = {}
test[1] = [1999,2000,2001]
test[2] = [440,441]
test[3] = [440,2000]

print invert(test)

This gives:

{2000: [1, 3], 2001: [1], 440: [2, 3], 441: [2], 1999: [1]}

I need to know if this can be done in-place, because my current strategy is exceeding the amount of physical memory on my machine with the dictionary I am working with. Can you think of a way to do it with generators?

  • 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-16T03:34:34+00:00Added an answer on May 16, 2026 at 3:34 am

    This doesn’t do it in place, but consumes oldDict by using popitem()

    from collections import defaultdict
    def invert(oldDict):
        invertedDict = defaultdict(list)
        while oldDict:
            key, valuelist = oldDict.popitem()
            for value in valuelist:
                invertedDict[value].append(key)
        return invertedDict
    

    I have a feeling that dict’s are never resized unless the size increases, so you may need to add+remove a dummy item periodically. See Shrinkage rate

    from collections import defaultdict
    def invert(oldDict):
        invertedDict = defaultdict(list)
        i=0
        while oldDict:
            key, valuelist = oldDict.popitem()
            for value in valuelist:
                invertedDict[value].append(key)
            i+=1
            if i%1000==0: # allow the dict to release memory from time to time
                oldDict[None]=None
                del oldDict[None]
        return invertedDict
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is Dictionary.Add() thread safe when you only insert? I've got a code that insert
do you know when you have that huge log table and you just need
I'm working with the sparse matrix, and I need to invert it. Actually, I'm
Need to insert selected text on the page into textarea. There must be some
In the dictionary file, which I am editing I often need to insert character
I need to test my data structure (in java) which is like a dictionary
I need to run a query that looks would look like INSERT INTO Appointments
I need to invert a BigInteger . Let's say i have BigInteger x; and
I need to do some comparing and shuffling of fields, (i come from a
I need a java program which should give all dictionary words from the give

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.