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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:53:47+00:00 2026-05-31T18:53:47+00:00

I can index a 2d numpy array with a tuple or even a list

  • 0

I can index a 2d numpy array with a tuple or even a list of tuples

a = numpy.array([[1,2],[3,4]])
i = [(0,1),(1,0)] # edit: bad example, should have taken [(0,1),(0,1)]
print a[i[0]], a[i]

(Gives 2 [2 3])

However, I can not manipulate the tuples with vector arithmetic, i.e.

k = i[0]+i[1]

does not give the desired (1,1) but concatenates.

On the other hand using numpy arrays for the indices, the arithmetic works, but the indexing does not work.

i = numpy.array(i)
k = i[0]+i[1]      # ok
print a[k]

gives the array [[3 4], [3 4]] instead of the desired 4.

Is there a way to do vector arithmetic on the indices but also be able to index a numpy array with them (without deriving a class from tuple and overloading all the operators)?

This question looked promising at first but I could not figure out if I can apply it to my situation.

Edit (comment on accepted answer):

… and working on arrays of indices then works as well using map

arr = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
ids = numpy.array([(0,1),(1,0)])
ids += (0,1) # shift all indices by 1 column
print arr[map(tuple,ids.T)]

(confusing to me why I need the transpose, though.
Would have run into this problem above as well,
and was just fortunate with [(0,1),(0,1)])

  • 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-31T18:53:48+00:00Added an answer on May 31, 2026 at 6:53 pm

    Yes. Convert the NumPy array to a tuple when you need to index:

    a[tuple(k)]
    

    Test:

    >>> a = numpy.array([[1,2],[3,4]])
    >>> i = numpy.array([(0,1),(1,0)])
    >>> k = i[0] + i[1]
    >>> a[tuple(k)]
    4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to index neighbours of a particular numpy array item. For example
Can I have that A-Z index bar in an UITableView also with UITableViewStyleGrouped, or
How can I know the (row, column) index of the minimum of a numpy
Let's say I have an numpy array A of size n x m x
I have a 2D array t in numpy: >>> t = numpy.array(range(81)).reshape((9,9)) >>> t
I have two NumPy arrays, e.g.: a = [1,2,3,4,5] and a filter array, e.g.:
I have the following 3 x 3 x 3 numpy array called a (the
How should one place links in a Flash app such that google can index
Is there any elegant way to exploit the correct spacing feature of print numpy.array
Say I have a 100 element numpy array. I perform some calculation on a

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.