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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:42:08+00:00 2026-06-01T23:42:08+00:00

Using list comprehension, itertools or similar functions, is it possible to create two unequal

  • 0

Using list comprehension, itertools or similar functions, is it possible to create two unequal lists from one list based on a conditional? Here is an example:

main_list = [6, 3, 4, 0, 9, 1]
part_list = [4, 5, 1, 2, 7]

in_main = []
out_main = []

for p in part_list:
    if p not in main_list:
        out_main.append(p)
    else:
        in_main.append(p)

>>> out_main
[5, 2, 7]

>>> in_main
[4, 1]

I’m trying to keep it simple, but as an example of usage, the main_list could be values from a dictionary with the part_list containing dictionary keys. I need to generate both lists at the same time.

  • 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-01T23:42:09+00:00Added an answer on June 1, 2026 at 11:42 pm

    A true itertools-based solution that works on an iterable:

    >>> part_iter = iter(part_list)
    >>> part_in, part_out = itertools.tee(part_iter)
    >>> in_main = (p for p in part_in if p in main_list)
    >>> out_main = (p for p in part_out if p not in main_list)
    

    Making lists out of these defeats the point of using iterators, but here is the result:

    >>> list(in_main)
    [4, 1]
    >>> list(out_main)
    [5, 2, 7]
    

    This has the advantage of lazily generating in_main and out_main from another lazily generated sequence. The only catch is that if you iterate through one before the other, tee has to cache a bunch of data until it’s used by the other iterator. So this is really only useful if you iterate through them both at roughly the same time. Otherwise you might as well use auxiliary storage yourself.

    There’s also an interesting ternary operator-based solution. (You could squish this into a list comprehension, but that would be wrong.) I changed main_list into a set for O(1) lookup.

    >>> main_set = set(main_list)
    >>> in_main = []
    >>> out_main = []
    >>> for p in part_list:
    ...     (in_main if p in main_set else out_main).append(p)
    ... 
    >>> in_main
    [4, 1]
    >>> out_main
    [5, 2, 7]
    

    There’s also a fun collections.defaultdict approach:

    >>> import collections
    >>> in_out = collections.defaultdict(list)
    >>> for p in part_list:
    ...     in_out[p in main_list].append(p)
    ... 
    >>> in_out
    defaultdict(<type 'list'>, {False: [5, 2, 7], True: [4, 1]})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I build a dict using list comprehension? I have two lists. series
Having a list like this: ['foo','spam','bar'] is it possible, using list comprehension, to obtain
I am generating these 2 lists using list comprehension. lists = ['month_list', 'year_list'] for
How to remove duplicate items from a list using list comprehension? I have following
I'm using a list of lists to store a matrix in python. I tried
I want to create a series of lists, all of varying lengths. Each list
I'm using Python 2.6, and I have two datasets, each being a list of
Is there a better way to express this using list comprehension? Or any other
I am trying to populate a list of 8 Ingredient objects using one list
Perhaps this a simple question with using list comprehension. How do you randomly select

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.