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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:19:48+00:00 2026-06-07T03:19:48+00:00

Problem part of the code: while counter < length: if something: else: for key,value

  • 0

Problem part of the code:

while counter < length:
    if something:
    else:
        for key,value in dictionary.items():
            if something:
            else:
    counter += 1

The structure is a { key:[ {key:value,key1:value1}, {key:value,key1:value1} ] }
which is what the counter (list index) is for.

So I’ve noticed this a few times in some scripts I’ve written while I’ve been teaching myself Python (3.2.3).

I tried searching but no luck (maybe my search terms are wrong, who knows?) and I’ve checked the documentation and what seems to be the problem is that for while statements it says, “The while statement is used for repeated execution as long as an expression is true”. And for For statements it says, “The expression list is evaluated once”.

Now when my script runs it keeps getting a key error because the counter has increased by 1; but the for statements isn’t updating to reflect the new elements from the list which is another dictionary; it’s still using the first element.

I assumed that each iteration in the while loop would cause the For statement to re-evaluate. Apparently I was wrong on that count.

Now I can rewrite the code, to fix this, but what I’d like to know as I’ve ran into this a few times already, is how can I use a For loop in a while statement in this fashion but have it re-evaluate the For statement on each iteration through the while loop.

Thanks!

Updated: I’ve added the actual code below. It wouldn’t surprise me if I’m just doing something wrong. I was watching the values in the debugger, and when count changed from 0 to 1, it started on the next iteration of the while loop. From there everything went as it should, until it hit the For loop “for key,value in item.items():”, and the values for item.items() didn’t change at all.
Updated Update: After trying to figure it out I’m pretty sure it’s the for item in v: that’s causing the problem. Let’s see if I can figure it out!

Fixed! Problem was with the For item in v: that was outside the while loop, solved by moving it into the loop.

def checkDifferences(newFile,oldFile):
    resultDiff = []
    for k,v in newFile.items():
        if newFile[k] != oldFile[k]:
            for item in v:
                count = 0
                lengthList = len(newFile[k])
                newKeys = newFile[k][count].keys()
                oldKeys = oldFile[k][count].keys()                
                while count < lengthList:
                    if newKeys == oldKeys:
                        for key,value in item.items():
                            newValue = newFile[k][count][key] 
                            oldValue = oldFile[k][count][key]
                            if newValue == oldValue:
                                pass
                            else:
                                resultDiff.append((k,count,key, oldValue,newValue))
                    else:
                        newValue = list(newFile[k][count].keys())
                        oldValue = list(oldFile[k][count].keys())
                        keyList = [key for key in newValue if key not in oldValue]
                        print(keyList)
                        for key,value in item.items():
                            if key in keyList:
                                oldValue = oldFile[k][count][oldKey]
                                resultDiff.append((k,count,key, None,newValue))
                            else:
                                oldValue = oldFile[k][count][key]
                            newValue = newFile[k][count][key]
                            if newValue == oldValue:
                                pass
                            else:
                                resultDiff.append((k,count,key, oldValue,newValue))
                            oldKey = key
                    count += 1
     return resultDiff
  • 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-07T03:19:49+00:00Added an answer on June 7, 2026 at 3:19 am

    The expression in the for statement is evaluated once each time the for loop begins. Since you’ve put the for loop into a while loop, each time around the while loop, when the for statement is encountered, the expression will be evaluated.

    There is something else wrong with your code. Post a real code sample.

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

Sidebar

Related Questions

I think problem is with wrong using function or something else. This part of
What seems to be the problem with this part of my code: while(c !=
I'm having a problem with a part of code. It should work,since it is
Strange problem happening where the end of these 2 blocks of code (this part:
my problem is the following: This is a part of my HTML-Code: <form method='GET'
i have a problem with my code. foreach (DataRow dr in dt_pattern.Rows) { part
Can't how to deal with my problem: part of view are hidden, when page
I've encountered a problem where part way through building a solution, VS 2010 becomes
I have created a dialog and got a problem that only part of the
Here is part 1 of our problem (Loading a dynamically generated XML file as

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.