Is there a way to remove too many if else conditions?
<p>I am currently making an interactive system using python, that is able to understand and reply. Hence for this there are lots of conditions for machine to analyze and process. For eg. take the following code(for reference only):</p>

<pre><code>    if ('goodbye') in message:                          
        rand = ['Goodbye Sir', 'Jarvis powering off in 3, 2, 1, 0']
        speekmodule.speek(rand,n,mixer)
        break

    if ('hello') in message or ('hi') in message:
        rand = ['Wellcome to Jarvis virtual intelligence project. At your service sir.']
        speekmodule.speek(rand,n,mixer)

    if ('thanks') in message or ('tanks') in message or ('thank you') in message:
        rand = ['You are wellcome', 'no problem']
        speekmodule.speek(rand,n,mixer)

    if message == ('jarvis'):
        rand = ['Yes Sir?', 'What can I doo for you sir?']
        speekmodule.speek(rand,n,mixer)

    if  ('how are you') in message or ('and you') in message or ('are you okay') in message:
        rand = ['Fine thank you']
        speekmodule.speek(rand,n,mixer)

    if  ('*') in message:
        rand = ['Be polite please']
        speekmodule.speek(rand,n,mixer)

    if ('your name') in message:
        rand = ['My name is Jarvis, at your service sir']
        speekmodule.speek(rand,n,mixer)
</code></pre>

<p>So, is there a way in which I can replace all these if else conditions?? Because there are much more conditions going to be, and it will make the execution slower. </p>

<p><code>if/elif/else</code> is a natural way to structure this kind of code in Python. As @imant noted, you may use dict-based approach in case of simple branching, but I see some mildly complex logic in your <code>if</code> predicates, so you'll have to check all predicates in any case and you won't have any performance gains with another code structure.</p>

<p>Though it may be a little bit more readable and easier to maintain if you factor out your predicates and actions like this:</p>

<pre><code>from collections import OrderedDict

def goodbye_p(message):
    return 'goodbye' in message

def goodbye_a():
    rand = ['Goodbye Sir', 'Jarvis powering off in 3, 2, 1, 0']
    # As @Moinuddin Quadri I also assume that your `speek` method
    # says random message from a list.
    # Otherwise you can use `random.choice` method 
    # to get a random message out of a list: `random.choice(messages)`.
    speekmodule.speek(rand, n, mixer)


def hello_p(message):
    return 'hello' in message or 'hi' in message

def hello_a():
    rand = ['Wellcome to Jarvis virtual intelligence project. At your service sir.']
    speekmodule.speek(rand, n, mixer)

# Use `OrderedDict` instead of `dict` to control order
# of checks and actions.
branches = OrderedDict([
    # (predicate as key, action as value)
    (goodbye_p, goodbye_a),
    (hello_p, hello_a),
])

for predicate, action in branches.items():
    if predicate(message):
        action_result = action()
        # You can add some logic here based on action results.
        # E.g. you can return some special object from `goodbye_a`
        # and then shut down Jarvis here.
        # Or if your actions are exclusive, you can add `break` here.
</code></pre>

<p>If all your predicates are the same and contain only substring checks, then it may be more performant to have tuples (e.g. <code>('hello', 'hi')</code>) as dict keys. Then you can iterate over those keys like this:</p>

<pre><code>for words, action in branches.items():
    if any(word in message for word in words):
        action()
</code></pre>

python