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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:45:07+00:00 2026-06-14T19:45:07+00:00

So I am stuck on nested loops, I feel like I understand them about

  • 0

So I am stuck on nested loops, I feel like I understand them about half the time and then I start to work on a different problem and then I don’t understand them anymore. Maybe I am over complicating things. Anyways back to my problem, I have an identity matrix

lst = [[1,0,0], [0,1,0], [0,0,1]]

I am trying to write a program that will check to see if it is an identity matrix, so I know that the index where i and j are have the same index(i.e i at position 0 and j at position 0; i at position 1 and j at position 1; i at position 2 and j at position 2) and are equal to 1 then the matrix is the identity matrix. Now my problem is I feel like I struggle articulating this to a computer. Or in other words lst[0][0], lst[1][1] and lst[2][2] should all equal 1 and all the other values should equal zero. Without giving me the answer can someone maybe nudge me in the right direction? I’ve been trying to solve this for about 2 weeks now I get am getting frustrated that I can’t since it seems so simple…

Thanks.

def identity(lst):
for i in lst:
    for j in i:
        if i == j and lst[i][j] == 1:
                if i != j and lst[i][j] == 0:
                    return True

return False

I am getting false where am I going wrong?

I think I got it!!

def identity(lst):
size = len(lst)
for i in range(len(lst)):
    if len(lst[i]) != size:
        return False
    for j in range(len(lst)):
        if i == j and lst[i][j] != 1:
            return False
        elif i != j and lst[i][j] != 0:
            return False

return True

Printing the i’s and j:

for i in range(len(lst)):
     for j in range(len(lst)):
         print("i:", i, "j:", j)

resulted in:

i: 0 j: 0
i: 0 j: 1
i: 0 j: 2
i: 1 j: 0
i: 1 j: 1
i: 1 j: 2
i: 2 j: 0
i: 2 j: 1
i: 2 j: 2

This really helped me a lot!

  • 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-14T19:45:08+00:00Added an answer on June 14, 2026 at 7:45 pm

    First thing first, your indentation seems broken. You should always indent correctly especially in python. It should be:

    def identity(lst):
        for i in lst:
            for j in i:
                if i == j and lst[i][j] == 1:
                    if i != j and lst[i][j] == 0:
                        return True
        return False
    

    Secondly, you’re not accessing the elements in the way you think you are. If I go ahead and print inside your loops with:

    for i in lst:
        for j in i:
            print "i:", i, "j:", j
        print
    

    I get:

    i: [1, 0, 0] j: 1
    i: [1, 0, 0] j: 0
    i: [1, 0, 0] j: 0
    
    i: [0, 1, 0] j: 0
    i: [0, 1, 0] j: 1
    i: [0, 1, 0] j: 0
    
    i: [0, 0, 1] j: 0
    i: [0, 0, 1] j: 0
    i: [0, 0, 1] j: 1
    

    You can use a combination of range (or xrange) and len functions if you need to iterate indices of the matrix.

    Lastly your conditionals doesn’t make sense this way

    if i == j and lst[i][j] == 1:
        if i != j and lst[i][j] == 0: # you never reach below here 
            return True               # because i == j is always true in here
                                      # provided by the first conditional
    

    You need to seperate those conditionals. Even if you do that you’ll get the a wrong answer because it will return True even if only one element satisfies the condition (i.e only one element is in the right place). I believe you need to think in the reverse way, return False in conditionals (and ofc don’t forget to change them respectively) and return True in the end if you don’t find any errors.

    Let me know how you progress, I can give you more hints..

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

Sidebar

Related Questions

i am working on a page wherein i have 2 nested loops, my problem
I stuck with problem of mail application , i am developing a website for
Im stuck with this little project in C# but basically my problem is this:
im stuck with a problem im basically trying to create a function where I
Have a program request the user to enter an uppercase letter. Use nested loops
I am stuck with what looks like a simple conceptual issue to me. After
I'm using Rails 2.3.2, and trying to get a nested object form to work
I'm stuck with a problem of the python wrapper for OpenCv. I have this
I'm new to rails and I'm stuck with a problem I can't seem to
I'm facing a problem with a nested route in Rails an I can't figure

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.