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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:56:59+00:00 2026-05-24T08:56:59+00:00

Is there a less verbose alternative to this: for x in xrange(array.shape[0]): for y

  • 0

Is there a less verbose alternative to this:

for x in xrange(array.shape[0]):
    for y in xrange(array.shape[1]):
        do_stuff(x, y)

I came up with this:

for x, y in itertools.product(map(xrange, array.shape)):
    do_stuff(x, y)

Which saves one indentation, but is still pretty ugly.

I’m hoping for something that looks like this pseudocode:

for x, y in array.indices:
    do_stuff(x, y)

Does anything like that exist?

  • 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-24T08:57:00+00:00Added an answer on May 24, 2026 at 8:57 am

    I think you’re looking for the ndenumerate.

    >>> a =numpy.array([[1,2],[3,4],[5,6]])
    >>> for (x,y), value in numpy.ndenumerate(a):
    ...  print x,y
    ... 
    0 0
    0 1
    1 0
    1 1
    2 0
    2 1
    

    Regarding the performance. It is a bit slower than a list comprehension.

    X = np.zeros((100, 100, 100))
    
    %timeit list([((i,j,k), X[i,j,k]) for i in range(X.shape[0]) for j in range(X.shape[1]) for k in range(X.shape[2])])
    1 loop, best of 3: 376 ms per loop
    
    %timeit list(np.ndenumerate(X))
    1 loop, best of 3: 570 ms per loop
    

    If you are worried about the performance you could optimise a bit further by looking at the implementation of ndenumerate, which does 2 things, converting to an array and looping. If you know you have an array, you can call the .coords attribute of the flat iterator.

    a = X.flat
    %timeit list([(a.coords, x) for x in a.flat])
    1 loop, best of 3: 305 ms per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a less brute-force way to do this? #!/bin/ksh THIS_SCRIPT=$(/usr/bin/readlink -f $(echo $0
I'm still learning Haskell, and I was wondering if there is a less verbose
I wonder if there is a less verbose way to do Input Verification in
Is there a less ugly/more programmatic way to solve this: ORDER BY CASE WHEN
Is there any library or framework to make JPA queries in a less verbose
Is there a prettier / less-verbose way to use iterators in C++? From the
Is there more elegant (less code) way of find a matrix OUT, with colSums(OUT)<=a
I was wondering if there is a less intrusive way to analyze a running,
There are more or less straightforward ways to emit or listen signals of existing
Is there a way to make mercurial use less to show me the logs/diffs/whatever

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.