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 search a dictionary for a value then delete that value

I have a dictionary where the values are lists. I want to search these for a specific value. right now it returns if the value is in each list individually but i just want overall then it deletes

Here’s what it returns right now:

marie true 
marie false 
marie false 
tom false 
tom true 
tom false 
jane false 
jane false 
jane false 

Here is what I want:

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

marie true 
tom true 
jane false

Here is the code:

dictionary = {'nyu': ['marie', 'taylor', 'jim'], 
              'msu': ['tom', 'josh'], 
              ' csu': ['tyler', 'mark', 'john']} 
              #made in different method in same class


class example:
    def get_names(self, name_list):
        for i in range(len(name_list)):
            for j in dictionary:
                if name_list[i] in dictionary[j]:
                    print('true')
                    dictionary[j].remove(name_list[i])
                else:
                    print('false')

def main():
    name_list = ['marie', 'tom', 'jane']
    e = example()
    e.get_names(name_list)

main()

>Solution :

You need to wait the iteration on all dict values before being able to yes yes or no

dictionary = {'nyu': ['marie', 'taylor', 'jim'],
              'msu': ['tom', 'josh'],
              ' csu': ['tyler', 'mark', 'john']}

def get_names(names):
    for name in names:
        name_found = False
        for dict_names in dictionary.values():
            if name in dict_names:
                name_found = True
                break
        print(name, name_found)

name_list = ['marie', 'tom', 'jane']
get_names(name_list)
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