-4

I have a dictionary example as:

dictionary = {Bay1: [False,False,True],
        Bay2: [True,True,True],
        Bay3: [True,True,False],
        Bay4: [False,False,False] }

What I'm trying to do is to go through the dictionary by checking each bay (Bay1, Bay2...) to see which one has an array where everything inside it is False. For that example, I want it to return 'Bay4'.

Secondly, I want to be able to check to see which is False and which is True in each bay by using loops. In other words, you can imagine that True represents being 'Booked' and False represents being free or 'Not booked'. I want to be able to check that for each bay and present it to the user in a good and easily readable format.

Taku
  • 31,927
  • 11
  • 74
  • 85
  • 2
    You need to show us what you have tried so far. For first question [try this](https://stackoverflow.com/questions/405516/if-all-in-list-something) – AstroMax Mar 13 '18 at 16:43

2 Answers2

0

You should provide some more information.

Anyway, if I understood your problem, you can use any() and all() in a for loop:

# print all Bay with all False:
for key, value in mydict:
    if not any(value):
        print key

# print all Bay with at least 1 False:
for key, value in mydict:
    if not all(value):
        print key
Gsk
  • 2,929
  • 5
  • 22
  • 29
0

First part

Returning key having value all False:

>>> d = {'Bay1': [False,False,True],
     'Bay2': [True,True,True],
     'Bay3': [True,True,False],
     'Bay4': [False,False,False]}

>>> ' '.join([k for k, v in d.items() if any(v) is False])
Bay4

Second part

Counting number of True (booked) and False (not booked) for each bay:

>>> d = {'Bay1': [False,False,True],
         'Bay2': [True,True,True],
         'Bay3': [True,True,False],
         'Bay4': [False,False,False]}

>>> '\n'.join(['{}: {} booked and {} not booked'.format(k, v.count(True), v.count(False)) for k, v in d.items()])
Bay1: 1 booked and 2 not booked                                
Bay2: 3 booked and 0 not booked                             
Bay3: 2 booked and 1 not booked                             
Bay4: 0 booked and 3 not booked
Austin
  • 25,759
  • 4
  • 25
  • 48