0

I am trying to solve a problem similar to this one below:

You are provided with a dictionary, for loop through all the values in the dictionary and check if they are equal to 45. If they are, delete them from the list. Below is the dictionary:

this_dict = {
 "dogs val":45, "sheep val":475, "cats val":33, "fish val":425, "crab val":11, "monkey val":45, "shark val":45,
 "badger val":45,"octopus val":55,"cheetah val":45,"owl val":656

I am then hoping to print and output the dictionary provided, but with all the key:value pairs with a value of 45 removed.

This is what I have tried thus far:

for i in this_dict:
 if this_dict[i] == 45:
 del this_dict[i]
khelwood
59.7k14 gold badges91 silver badges116 bronze badges
asked Jan 13, 2023 at 15:29
2
  • What is the output that you get vs the expected output? Commented Jan 13, 2023 at 15:31
  • Try making the title of the post more descriptive of your problem Commented Jan 13, 2023 at 15:32

4 Answers 4

1

Why not filter in a dict comprehension ?

Rather than deleting in original dict, you create a new one and can reassign to your original dict variable.

{k: v for k, v in this_dict.items() if v != 45}

gives

{'sheep val': 475,
 'cats val': 33,
 'fish val': 425,
 'crab val': 11,
 'octopus val': 55,
 'owl val': 656}

Full demo

this_dict = {
 "dogs val": 45,
 "sheep val": 475,
 "cats val": 33,
 "fish val": 425,
 "crab val": 11,
 "monkey val": 45,
 "shark val": 45,
 "badger val": 45,
 "octopus val": 55,
 "cheetah val": 45,
 "owl val":656
}
id(this_dict) # 4478365376
this_dict = {k: v for k, v in this_dict.items() if v != 45}
id(this_dict) # 4474822144 different id, it's a new dict
answered Jan 13, 2023 at 15:35
Sign up to request clarification or add additional context in comments.

Comments

0

I guess you got a RuntimeError because you iterate on the dictionary which changes. If you create a list with the keys of the dict, you do not have this problem:

this_dict = {"dogs val":45, "sheep val":475, "cats val":33, "fish val":425, "crab val":11, "monkey val":45, "shark val":45, "badger val":45,"octopus val":55,"cheetah val":45,"owl val":656}
for i in list(this_dict.keys()):
 if this_dict[i] == 45:
 del this_dict[i]
answered Jan 13, 2023 at 15:35

Comments

0

Your code did not work because you cannot modify the same dictionary you are iterating on. You receive that error:

RuntimeError: dictionary changed size during iteration

You can solve it using list, that make a copy of the dictionary:

for key in list(this_dict):
 if this_dict[key] == 45:
 del this_dict[key]

It would be a much more pythonic solution to use a dict comprehension, but using for loops was a requirement in the question.

answered Jan 13, 2023 at 15:35

Comments

-1

You should try the following:

for key in this_dict.keys():
 if this_dict[key] == 45:
 del this_dict[key]

Let me know if you have any questions.

answered Jan 13, 2023 at 15:36

1 Comment

Why get val out to use this_dict[key] ?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.