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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:27:37+00:00 2026-06-11T20:27:37+00:00

A simply example of numpy indexing: In: a = numpy.arange(10) In: sel_id = numpy.arange(5)

  • 0

A simply example of numpy indexing:

In: a = numpy.arange(10)
In: sel_id = numpy.arange(5)
In: a[sel_id]
Out: array([0,1,2,3,4])

How do I return the rest of the array that are not indexed by sel_id? What I can think of is:

In: numpy.array([x for x in a if x not in a[id]])
out: array([5,6,7,8,9])

Is there any easier way?

  • 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-11T20:27:38+00:00Added an answer on June 11, 2026 at 8:27 pm

    For this simple 1D case, I’d actually use a boolean mask:

    a = numpy.arange(10)
    include_index = numpy.arange(4)
    include_idx = set(include_index)  #Set is more efficient, but doesn't reorder your elements if that is desireable
    mask = numpy.array([(i in include_idx) for i in xrange(len(a))])
    

    Now you can get your values:

    included = a[mask]  # array([0, 1, 2, 3])
    excluded = a[~mask] # array([4, 5, 6, 7, 8, 9])
    

    Note that a[mask] doesn’t necessarily yield the same thing as a[include_index] since the order of include_index matters for the output in that scenario (it should be roughly equivalent to a[sorted(include_index)]). However, since the order of your excluded items isn’t well defined, this should work Ok.


    EDIT

    A better way to create the mask is:

    mask = np.zeros(a.shape,dtype=bool)
    mask[include_idx] = True
    

    (thanks to seberg).

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

Sidebar

Related Questions

Given a self-indexing (not sure if this is the correct term) numpy array, for
I think the easiest way to explain is simply linking to an example of
Given a String that is simply a day, for example, Thu or Thursday, how
OK, I think this will be fairly simple, but my numpy-fu is not quite
can someone point out a simple example of making a Python package using distutils
Many array methods return a single index despite the fact that the array is
The docs simply say or define custom schema (non-juggling), for example, mongoose. Please note,
Ok, I'm very simply trying to take this example... http://jsfiddle.net/shanabus/HGF59/ and put it on
testlist is just a list of objects. For example testlist.0.name is simply Test3 I
I have a 2D numpy array of shape (N,2) which is holding N points

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.