1

I have a dictionary called Basket #of fruit. For reasons, Basket looks like this..

Basket = {
          Fruit1 : "none"
          Fruit2 : "none"
          Fruit3 : "none"
          Fruit4 : "none"
          Fruit5 : "none"
         }

I'd like to check if Apple is in this dictionary, and if it is not- to enter it as Fruit1. But lets say the Basket has been accessed already somehow and Banana has already been set as Fruit1, then I'd like Apple to be set as Fruit2 instead, but if Pear is already in there than it should be Fruit3 and so on. My overall code is... not the best, but at this point this is the way it must work if it is to work, so short of scrapping what is in place already (I hope to revise it all later) how can I make this work?

At the moment the rest of the code simply checks if Fruit1 == Apple and if not moves on to compare Fruit2 etc, if it finds a match then it does stuff but if there is no Apple already in Basket then Apple will never be added, and all keys in the Basket are initially set to "none". I've perplexadoxed myself. Any advise appreciated!

chepner
  • 497,756
  • 71
  • 530
  • 681
SamChancer
  • 75
  • 5
  • 2
    You might want to use a `list` instead of a `dict` and simply append items if they are missing from the list. – a_guest Nov 12 '15 at 12:00
  • You could look at https://stackoverflow.com/questions/8023306/get-key-by-value-in-dictionary and https://stackoverflow.com/questions/8214932/how-to-check-if-a-value-exists-in-a-dictionary-python. – Mike P Nov 12 '15 at 12:03
  • Are the keys supposed to be the strings `'Fruit1'`, `'Fruit2'`, and so on, or some variables with certain values? – dorverbin Nov 12 '15 at 12:10
  • So if I try Fruit = Apple, if Fruit not in Basket.iteritems(): Basket["none"] = Fruit break? Will this set the first Fruit# with value "none" to Apple and then break off? – SamChancer Nov 12 '15 at 12:22
  • Donald V, the keys are indeed strings. :( – SamChancer Nov 12 '15 at 12:23
  • Are you just using the dict for its keys (i.e., the values are always "none")? The just use a set: `basket = set()`. You can check `if "apple" not in basket:` and put an apple in the basket with `basket.add("apple")`. – chepner Nov 12 '15 at 12:30

2 Answers2

2

The following statement:

'apple' in Basket.values()

will return True if 'apple' is one of the values in the dictionary.

Basket.values().index('apple')

returns the index of 'apple'. If 'apple' isn't in the dictionary, you get a ValueError exception.

If you get a ValueError exception, add 'apple' as the 'Fruit1' value; otherwise, use the index to set the correct FruitN value:

index = Basket.values().index('apple')
key, value = Basket.items()[index]
value = 'banana'
Basket[key] = value

Note, the above snippet shows how to replace an existing value of 'apple' with 'banana'; it doesn't show the exception handling in case 'apple' isn't in the dictionary. However, it should be enough to get you moving.

Mike P
  • 742
  • 11
  • 26
1

I believe something like this will work. There are probably more efficient ways to do it though:

Basket = {
          'Fruit1' : "none",
          'Fruit2' : "none",
          'Fruit3' : "none",
          'Fruit4' : "none",
          'Fruit5' : "none"
         }

basketSize = len(Basket)

if 'apple' not in Basket.values():
    print "Couldn't find the apple"

    for i in range(basketSize):
        curItem = "Fruit"+str(i+1)
        if Basket[curItem] == "none":
            Basket[curItem] = "apple"
            break

print Basket
Simon
  • 9,762
  • 15
  • 62
  • 119
  • Thanks, this answer suits most my generally "probably more efficient ways" code and also tells me how to take variables with strings as a name and be smarter with them. – SamChancer Nov 12 '15 at 12:29