Skip to content Skip to sidebar Skip to footer

Python Dictionary - Value

I have an if block statement that is checking if a string that I have assigned to variable movieTitle contains the value of a key-value pair in a predefined dictionary. The code I

Solution 1:

You can do as Captain Skyhawk suggests, or you can replace your entire if condition with:

ifany(movieTitle.find(leaveOut[c])<1forcin'abcdefghijklm'):

As for your second question, are you sure you don't mean

if not any(movieTitle.find(leaveOut[c])<1forcin'abcdefghijklm'):

Solution 2:

I believe you should be using 'or'. It appears you're using a binary or( the | character).

For example:

if ((movieTitle.find(leaveOut['a']) < 1) or
    (movieTitle.find(leaveOut['b']) < 1) or
    (movieTitle.find(leaveOut['c']) < 1) or ....

Solution 3:

Could you confirm exactly what you're trying to achieve here? You're trying to execute a set of instructions if ANY of the values in the leaveOut dictionary is NOT present in the movieTitle? If so:

if [x for x in leaveOut.values() if x not in movieTitle]:

would be more concise. Also, if you're going to use the formulation above then the comparator has to be 0 rather than 1 otherwise matches at the first character will fire the set of instructions.

Post a Comment for "Python Dictionary - Value"