0

So I want to create a while loop that will stop once all the Booleans inside a two dimensional list are true (a dictionary specifically).

This is the dictionary I am trying to query.

tasks = {'bathroom':
         {'linus':False, 
         'ola': False, 
         'lex': False, 
         'lotte': False, 
         'yoan': False, 
         'daan': False}, 
    'kitchen':
         {'linus':False, 
         'ola': False, 
         'lex': False, 
         'lotte': False, 
         'yoan': False, 
         'daan': False},
    'garbage':
         {'linus':False, 
         'ola': False, 
         'lex': False, 
         'lotte': False, 
         'yoan': False, 
         'daan': False},
    'recycling':
         {'linus':False, 
         'ola': False, 
         'lex': False, 
         'lotte': False, 
         'yoan': False, 
         'daan': False},
    'corridors':
         {'linus':False, 
         'ola': False, 
         'lex': False, 
         'lotte': False, 
         'yoan': False, 
         'daan': False}}

This is what I have tried to come up with but it doesn't seem to work

while not all(done == true for done in names.values() for names in tasks.values())
  • I think your statement should work except that the ordering is wrong. It should be `while not all(done for names in tasks.values() for done in names.values())`. Check out https://stackoverflow.com/a/36734643/12345551 – He3lixxx May 14 '21 at 12:53

2 Answers2

1

What He3lixxx said is correct; the comprehension was in the wrong order.

while not all(done for names in tasks.values() for done in names.values())

Think of it in terms of normal Python workflow, except that the value is at the top, instead of inside the nested loop

while not all(done                             # value
              for names in tasks.values()      # outer loop
                  for done in names.values()   # inner loop
             )

But if all values are actually boolean, you can shorten it to

while not all(all(task.values()) for task in tasks.values())

If you were to write out the second approach, it would

while not all(all(subtask for subtask in task.values()) for task in tasks.values())

Which is similar to the ordering you had, but you have to remember the all(...) part is a single value. So you're only doing "value-outer loop", regardless of whether the "value" is another comprehension list itself.

Reti43
  • 8,010
  • 3
  • 22
  • 40
0

hi there green i paste you're code in my ide and getting some errors, the first problem i found is that your true boolean isn't suposed to be types with a capital T so it's not "true" but "True", and the second problem i see is that you're trying to solve the problem in 1 line with a while loop, while loops usualy need a ":" and can't be used in 1 line, sadly i didn't find a way to mutate your data, but i did find a way to access it, here's the code you can delete the comments later:

finished = True
while finished:
    
    '''
    access the first dict that is the task's dict 
    aka kitchen, corridors, bathroom, ect, but it haven't find the names
    '''
    # this comment on top of this comment is for the first for loop
    for task in tasks.values():
        '''
        find the names in every task and prints them, 
        you can change the print statement to a line that mutates the data
        '''
        # this comment is for the second for loop
        for names in task.values():
            print(names)  # prints the data that got access, you can change this later

    print(tasks)  # prints the whole dict
    finished = False

hoped i helped you a bit and delete the trouble of getting access to the data ^w^