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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:21:25+00:00 2026-05-31T17:21:25+00:00

What is the equivalent of the following in python? for (i=0; i<n; i++) for

  • 0

What is the equivalent of the following in python?

for (i=0; i<n; i++)
    for (j=i+1; j<n; j++)
        //do stuff with A[i], A[j]

Or in some sense, the following. It should also remove the element from A at the completion of each round of the loop.

for a in A:
    for a' in A/{a}: #i.e. rest of the elements of A
        #do something with a,a'
    #remove a from A

Is there a pythonic way of doing this without using enumerate()?

Edits:

Sorry for the bad description.

  1. In the first example, I mean to use i & j only as indices. Their values do not matter. Its just a rough c++ equivalent of the latter.

  2. The outer loop is executed n times. The inner loop is executed (n-1), (n-2)…0 times for each iteration of the outer loop.

Maybe this might help (pseudocode):

function next_iteration(list):
    head = first element
    tail = remaining elements #list
    each element in tail interacts with head one by one
    next_iteration(tail)

PS: All code samples above are pseudocodes. I’m trying to express something that is still a bit vague in my mind.

  • 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-31T17:21:26+00:00Added an answer on May 31, 2026 at 5:21 pm

    Since your two questions are different, here is solution for your second problem:

    for i in xrange(len(A)):
        for j in xrange(len(A)):
            if i != j:
                do_stuff(A[i], A[j])
    

    or using itertools (I think using the included batteries is very pythonic!):

    import itertools
    
    for a, b in itertools.permutations(A, 2):
        do_stuff(a, b)
    

    This applies do_stuff to all combinations of 2 different elements from A. I you want to store the result just use:

    [do_stuff(a, b) for a, b in itertools.permutations(A, 2)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the Python equivalent of the following code in Ruby? def loop cont=nil
Why arn't the following two scripts equivalent? (Taken from another question: Understanding Python Decorators
Is there an equivalent python function for each of the following (maybe in the
To do the equivalent of Python list comprehensions, I'm doing the following: some_array.select{|x| x
In Python, how do I perform the equivalent of the following import http.client but
For some types in Python, the is operator seems to be equivalent to the
The following Python List Comprehension statement could be rephrased as the for loop below.
What is the equivalent of the following python code map(func, list) in vala?
I need a ruby equivalent for the following python code import array my_array =
What is the Clojure equivalent (for the exact algorithm) of the following Python code?

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.