Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to stop dict.pop("foo") from deleting every dict item with name the "foo"?

Here’s my code, it’s a simple classification program for animals.

horse = {
        "name": "Horse",
        "legs": 4,
        "land": "yes",
        "pet": "yes",
        "stripe": "no"
    }

dolphin = {
        "name": "Dolphin",
        "legs": 0,
        "land": "no",
        "pet": "no",
        "stripe": "no"
    }

userIn = dict()
userIn["legs"] = int(input("How many legs does it have? "))
userIn["land"] = input("Is it a land animal (yes/no)? ")
userIn["pet"] = input("Is it a pet? ")
userIn["stripe"] = input("Does it have stripes? ")

animals = [horse, dolphin]

for animal in animals:
    bak = animal
    bak.pop("name")
    print(bak)
    print(animal)
    if bak == userIn:
        print(animal["name"])

But, at the end where I say bak.pop("name"), it also removes "name" from animal.

How do I make it just remove "name" from bak and not animal?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this;

for animal in animals:
    bak = animal.copy() #edit here
    bak.pop("name")
    print(bak)
    print(animal)
    if bak == userIn:
        print(animal["name"])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading