A single liner using list comprehension.
[sent.replace(currentWord,newWord) if sent.find(currentWord)>=0 else sent.replace(newWord,currentWord) for sent in sentList]
So,
IN: sentList = ['i like math', 'i am a cs expert']
IN : currentWord = "math"
IN : newWord = "cs"
OUT : ['i like cs', 'i am a math expert']
Here, if sent.find('math')>=0 will find out if the string conntains 'math' , and if so, replaces it with 'cs', else it replaces 'cs' with 'math'. In case the string contains neither, then too it will print the original, as replace only works if the substring is found.
Edit : as @Rawing pointed out, there were a few bugs in the above code. So here is the new code that will handle every and all cases.
I have used re.sub to handle replacement of only words, and the replacement algorithm is that of how you would swap two variables, say for x and y, where we introduce a temporary variable t to help with the swap: t = x; x = y; y = t. This approach was chosen since we have to do Multiple substitution at the same time.
from re import sub
for s in sentList:
#replace 'math' with '%math_temp%' in s (here '%math_temp%' is a dummy placeholder that we need to later substitute with 'cs')
temp1 = sub(r'\bmath\b','%math_temp%' , s)
#replace 'cs' with 'math' in temp1
temp2 = sub(r'\bcs\b','math', temp1)
#replace '%math_temp%' with 'cs' in temp2
s = sub('%math_temp%','cs', temp2)
print(s)
So this time on a bigger test case, we get :
IN : sentList = ['i like math', 'i am a cs expert', 'math cs', 'mathematics']
IN : currentWord = "math"
IN : newWord = "cs"
OUT : i like cs
i am a math expert
cs math
mathematics