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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:14:09+00:00 2026-05-24T05:14:09+00:00

Is there any elegant way of splitting a list/dict into two lists/dicts in python,

  • 0

Is there any elegant way of splitting a list/dict into two lists/dicts in python, taking in some arbitrary splitter function?

I could easily have two list comprehensions, or two selects, but it seems to me there should be some better way of doing it that avoids iterating over every element twice.

I could do it easily with a for loop and if statement, but that takes something like 7 lines of code for what should be a very simple operation.

Any ideas?

Edit:

Just for reference, my two solutions would be,

# given dict cows, mapping cow names to weight
# fast solution
fatcows = {}
thincows = {}
for name, weight in cows:
    if weight < 100:
        thincows[name] = weight
    else:
        fatcows[name] = weight

# double-list-comprehension solution would be
fatcows = {name: weight for name, weight in cows.items() if weight > 100}
thincows = {name: weight for name, weight in cows.items() if weight < 100}

I was thinking there must be something more elegant than this that i never thought of, something like:

thincows, fatcows = ??? short expression involving cows ???

I know it’s possible to do by writing higher order functions stuff to do it for me, and i know how to do it manually. I was just wondering if there was some super-elegant language feature to do it for me.

It’s like you can write your own subroutines and whatnot to do a SELECT on a list, or you can just say

thincows = select(cows, lambda c: c.weight < 100)

I was hoping there would be some similarly elegant way of splitting the list, with one pass

  • 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-24T05:14:09+00:00Added an answer on May 24, 2026 at 5:14 am

    More fun with cows 🙂

    import random; random.seed(42)
    cows = {n:random.randrange(50,150) for n in 'abcdefghijkl'}
    
    thin = {}
    for name, weight in cows.iteritems():
        thin.setdefault(weight < 100, {})[name] = weight
    
    >>> thin[True]
    {'c': 77, 'b': 52, 'd': 72, 'i': 92, 'h': 58, 'k': 71, 'j': 52}
    
    >>> thin[False]
    {'a': 113, 'e': 123, 'l': 100, 'g': 139, 'f': 117}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any elegant way of turning [$(div), $(span), $(li)] into $(div, span, li)
In java is there any elegant way to compare two different StringBuilders and copy
Is there any elegant way of applying a certain style to all <input type=text>
Is there any way to check whether a file is locked without using a
Is there any way to capture the MouseDown even from the .NET 2.0 TextBox
Is there any known way of listing the WMI classes and their properties available
Is there any way to apply an attribute to a model file in ASP.NET
Is there any way to use boolean algebra in JS? Eg I would like
Is there any free or commercial component written in .NET (no COM interop) that
Is there any query which can return me the number of revisions made to

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.