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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:42:33+00:00 2026-06-12T03:42:33+00:00

I find myself frequently making indexed lists from flat ones in Python. This is

  • 0

I find myself frequently making indexed lists from flat ones in Python. This is such a common task that I was wondering if there’s a standard utility that I should be using for it.

The context is this: given an array, I need to create a dict of smaller arrays using some key for grouping.

e.g:
["Andy","Alice","Bob","Beth","Charlie"] becomes
{"A":["Andy","Alice"],"B":["Bob","Beth"],"C":["Charlie"]}

My solution looks like this:

def make_index(data,key,value=lambda x:x):
    d={}
    for item in data:
        k = key(item)
        v = value(item)
        try: d[k].append(v)
        except KeyError: d[k]=[v]
    return d

It’s simple and all, but am I reinventing something that is implemented better elsewhere?

  • 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-12T03:42:35+00:00Added an answer on June 12, 2026 at 3:42 am

    You can do the same a little simpler with a defaultdict:

    from collections import defaultdict
    
    def make_index(data,key,value=lambda x:x):
        d=defaultdict(list)
        for item in data:
            d[key(item)].append(value(item))
        return d
    

    Using a defaultdict is faster than using .setdefault(), which would be the other option.

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

Sidebar

Related Questions

I find myself frequently repeating HTML code to format items such as lists, tables,
I have this website, where I frequently find myself trying to pass information from
When creating reports I find myself frequently joining to ad-hock sub queries like this:
I frequently find myself creating classes which use this form (A): abstract class Animal
I frequently find myself writing utility classes that can be re-used throughout my projects.
I frequently find myself making a dummy console app to test something simple out.
I find myself frequently writing code like this in the HTML HEAD and other
I find myself calling functions from lambdas frequently as the provided delegate does not
I frequently find myself writing max value functions that search through an array of
I frequently find myself wanting to change something like this, in a layout file:

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.