-1

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:

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()
Squilliam
  • 31
  • 5
  • 1
    `nyu` and `msu` are not valid dictionary keys. Also, there's an indentation issue with `self._dictionary`. And what is `self`? That's usually seen inside a class. Please [edit] your question and make sure you are showing us accurate code. The easiest way to do that is to copy your code, paste it into the editor, then select it and click the `{}` button or press Ctrl+K. We shouldn't have to guess at what your code actually does. – ChrisGPT was on strike Nov 23 '22 at 19:19
  • yeah, i know that's where the problem is I don't know how i would remove the name from the lists if I don't know I – Squilliam Nov 23 '22 at 19:27
  • You may want to read [Strange result when removing item from a list while iterating over it](https://stackoverflow.com/q/6260089/354577) – ChrisGPT was on strike Nov 23 '22 at 19:28

2 Answers2

1

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)
azro
  • 53,056
  • 7
  • 34
  • 70
0

You must not remove from something you are iterating. NEVER!
But you may iterate a copy while deleting from the original, as in:

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

class example:
    def remove_names( self, name_list):
        dic = dictionary.copy()
        for name in name_list:
            for k in dic:
                if name in dic[k]:
                    dictionary[k].remove( name)

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

main()

It will print:
{'nyu': ['taylor', 'jim'], 'msu': ['josh'], ' csu': ['tyler', 'mark', 'john']}

user3435121
  • 633
  • 4
  • 13