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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:19:20+00:00 2026-06-18T15:19:20+00:00

Basically what I want to do is take two lists of objects and based

  • 0

Basically what I want to do is take two lists of objects and based on some test and divide them into two lists depending on whether the result is True or False. Sort of what filter() does, except that instead of determining if it’s in or out it determines which list/iterator to go to. I’ve made this attempt using itertools.groupby():

import random
from itertools import groupby

class FooObject(object):
    def __init__(self):
        self.key = random.choice((True, False))

    def __repr__(self):
        return '<Foo: %s>' % self.key

foos = [FooObject() for _ in range(10)]
left, right = [], []

groups = groupby(sorted(foos, key=lambda o: o.key), lambda o: o.key)
for k, group in groups:
    if k:
        right = list(group)
    else:
        left = list(group)

print left
print right

This gets the job done, but just wondering if there is a clearner/simpler way. I realize I could use filter() (or the equivalent list comprehension) and do it in two passes, but what fun is that?

  • 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-18T15:19:22+00:00Added an answer on June 18, 2026 at 3:19 pm

    If you only have 2 buckets, you can use a ternary:

    d={'left':[],'right':[]}
    for e in (random.random() for i in xrange(50)):
        d['left' if e<0.5 else 'right'].append(e)
    

    With more than 2 buckets, use a function that returns keys already defined or use a default dict with a list:

    def f(i):
       return int(i*10)
    
    DoL=defaultdict(list)
    for e in (random.random() for i in xrange(50)):
       DoL[f(e)].append(e)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my LinearLayout I have two child ViewGroups, basically I want them to take
I basically want to take two images taken from the camera on the iPhone
Basically I have a two databases on SQL Server 2005. I want to take
I want to take two tables and just put them next to each other
I have a partial view that I want to basically take care of itself
I want a function that takes two lists of any type and returns one
Basically I want to take an image that the user chooses from their photo
Basically I want to take all the names (RequiredAccount, RequiredContact, RequiredOwner) for all the
I've got some 2D geometry. I want to take some bounding rect around my
I want to take two value from user i.e name and location and then

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.