pythondictionaryfor-loopexact-match

Trouble converting "for key in dict" to == for exact matching


Good morning,

I am having trouble pulling the correct value from my dictionary because there are similar keys. I believe I need to use the == instead of in however when I try to change if key in c_item_number_one: to if key == c_item_number_one: it just returns my if not_found: print("Specify Size One") however I know 12" is in the dictionary.

c_item_number_one = ('12", Pipe,, SA-106 GR. B,, SCH 40, WALL smls'.upper())
print(c_item_number_one)
My formula is as follows:

def item_one_size_one():
    not_found = True
    for key in size_one_dict:
        if key in c_item_number_one:
            item_number_one_size = size_one_dict[key]
            print(item_number_one_size)
            not_found = False
            break
    if not_found:
            print("Specify Size One")
item_one_size_one()

The current result is:

12", PIPE,, SA-106 GR. B,, SCH 40, WALL SMLS
Specify Size One

Solution

  • There are two usages of the word in here - the first is in the context of a for loop, and the second entirely distinct one is in the context of a comparison.

    In the construction of a for loop, the in keyword connects the variable that will be used to hold the values extracted from the loop to the object containing values to be looped over.

    e.g.

    for x in list:
    

    Meanwhile, the entirely distinct usage of the in keyword can be used to tell python to perform a collection test where the left-hand side item is tested to see whether it exists in the rhs-object's collection.

    e.g.

    if key in c_item_number_one:
    

    So the meaning of the in keyword is somewhat contextual.

    If your code is giving unexpected results then you should be able to replace the if-statement to use an == test, while keeping everything else the same.

    e.g.

    if key == c_item_number_one:
    

    However, since the contents of c_item_number_one is a tuple, you might only want to test equality for the first item in that tuple - the number 12 for example. You should do this by indexing the element in the tuple for which you want to do the comparison:

    if key == c_item_number_one[0]:
    

    Here the [0] is telling python to extract only the first element from the tuple to perform the == test.

    [edit] Sorry, your c_item_number_one isn't a tuple, it's a long string. What you need is a way of clearly identifying each item to be looked up, using a unique code or value that the user can enter that will uniquely identify each thing. Doing a string-match like this is always going to throw up problems.

    There's potential then for a bit of added nuance, the 1st key in your example tuple is a string of '12'. If the key in your == test is a numeric value of 12 (i.e. an integer) then the test 12 == '12' will return false and you won't extract the value you're after. That your existing in test succeeds currently suggests though that this isn't a problem here, but might be something to be aware of later.