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 question.(5)

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

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Quick handling of the runtimeerror: dictionary changed size during iteration error
Next
Answered
Adam Huguet
  • 20
Adam Huguet
Asked: May 12, 20222022-05-12T09:09:14+00:00 2022-05-12T09:09:14+00:00In: python

Quick handling of the runtimeerror: dictionary changed size during iteration error

  • 20

. Advertisement .

..3..

. Advertisement .

..4..

For the “runtimeerror: dictionary changed size during iteration” problem. I tried to fix it, but It doesn’t work and returns the result I want. Here is my program:

d = {'a': [1], 'b': [1, 2], 'c': [], 'd':[]}
for i in d:
  if not d[i]:
  d.pop(i)

and

RuntimeError: dictionary changed size during iteration

has occurred. I’ve checked the entire command line but still can’t find the mistake.

runtimeerror
  • 2 2 Answers
  • 54 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-01T11:18:04+00:00Added an answer on June 1, 2022 at 11:18 am

    The cause: The reason for this error is that while a dictionary changing during for loop, you can not  iterate through it.

    Solution: Let’s make a casting to list and iterate over that list as below:

    for key in list(d): 
             if not d[key]: 
                    d.pop(key)
    • 24
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Mia Guy
    2022-05-25T19:37:13+00:00Added an answer on May 25, 2022 at 7:37 pm

    Only copy is required

    You can iterate over the dictionary fields and change the desired d on the fly. It works with every Python version so it’s easier to understand.

    In [1]: d = {'a': [1], 'b': [1, 2], 'c': [], 'd':[]}
    
    In [2]: for i in d.copy():
     ...: if not d[i]:
     ...: d.pop(i)
     ...: 
    
    In [3]: d
    Out[3]: {'a': [1], 'b': [1, 2]}

    (BTW) Generally, to iterate over the copy of your data structures, instead of .copy for dictionary or slicing [:] for lists you can use import copy->copy.copy (for shallow copies which are equivalent to copy that’s supported by dictionaries, or slicing [:] that’s supported by lists) on your data structure.

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

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

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.