-5

So lets say I have this

Sentence = "A surprise to be sure but a welcome one"
keyword_list = ['surprise', 'welcome', 'one']

def or_maker(sentence): 
    for i in range(len(keyword_list)):
        if keywordlist[i] in Sentence:
            return keywordlist[i] or keywordlist[i + 1] or keyword_list[i + 2] or ... etc.

So hopefully when I call the function or_maker(Sentence) it returns to me

'surprise' or 'welcome' or 'one'

Is this possible in Python?

Ali Halawa
  • 1
  • 1
  • 5
  • Why dont you want to return the string array and then apply the operations to the result? – Simas Joneliunas Jul 07 '18 at 05:59
  • What do you expect to happen when the sentence is "A blah-blah to be sure but a welcome one"? – DYZ Jul 07 '18 at 06:02
  • 2
    The result of `'surprise' or 'welcome' or 'one'` is `'surprise'`. Is that the result you're looking for? – Ashish Acharya Jul 07 '18 at 06:08
  • Not that I don't want to do that, I am just wondering if the above problem is possible – Ali Halawa Jul 07 '18 at 06:10
  • 1
    No that isn't what I am looking for, I am just wondering if I can return strings with operators like or in between them – Ali Halawa Jul 07 '18 at 06:12
  • It is not possible to return **anything** "with operators" **of any kind** "between them". What is returned is **the result of evaluating** that code. Just like how if you write `return 4 * 3`, it returns the integer `12`. – Karl Knechtel Oct 17 '22 at 05:11

4 Answers4

1

The pythonic way of saying

return keywordlist[i] or keywordlist[i + 1] or keyword_list[i + 2] or ... etc.

is

return next(filter(bool, keyword_list[i:]), None)

Or even slightly better as @UltraInstinct has suggested

return next(filter(bool, keyword_list[i:-1]), keyword_list[-1])

Both would return the first non-empty value

Sunitha
  • 11,777
  • 2
  • 20
  • 23
  • Upvoted! Although technically, exact equivalent would be: `next(filter(bool, keyword_list[:-1]), keyword_list[-1])` so that the result take an appropriate false-y (`0` vs `""` vs `[]` etc) value when all are false-y, but then it would fail when `keyword_list` is empty. – UltraInstinct Jul 07 '18 at 06:33
  • @UltraInstinct. Makes sense. Updated my answer – Sunitha Jul 07 '18 at 06:37
-1

You can do this with an explicit loop:

result = False
for value in keywordlist[i:]:
    result = result or value
return result

If you want to preserve the short-circuit semantics of or:

result = False
for value in keywordlist[i:]:
    result = result or value
    if result: return result
return result

You can also turn this into a call to reduce, or a recursive function instead of a loop (either of which could be written as an ugly one-liner, if that’s important to you), but both of those are probably less Pythonic.

But the best way to do it is probably to use any:

return any(keywordlist[i:])

However, note that any returns True if any of the values are truthy (for strings, this means non-empty), False otherwise, while a chain of ors will return the first truthy value if any are truthy, or the last falsely value if none are. So, if you want the actual string values, this doesn’t do the same thing.

abarnert
  • 354,177
  • 51
  • 601
  • 671
-2
keyword_list = ['surprise', 'welcome', 'one']
sentence = "A surprise to be sure but a welcome one"

def or_maker(sentence): 
    for i in range(len(keyword_list)):
        if keyword_list[i] in sentence:
            returnlist = " || ".join(keyword_list)
            print(returnlist)

or_maker(sentence)
user9862376
  • 29
  • 10
-3

what you are looking for is the join() function

returnlist = " or ".join(keywordlist)
return returnlist

which returns a single string "surprise or welcome or one"

Or if you want it with the quotation marks:

returnlist = "'" + "' or '".join(keywordlist) + "'"
return(returnlist)
Reroute
  • 265
  • 1
  • 9
  • But I want the operator or, not or as a string. Is that possible? – Ali Halawa Jul 07 '18 at 06:06
  • At a guess you want to pass this to a testing function elsewhere, if so then you could wrap it in an eval(), perhaps better clarifying where you want to use this result can help clarify things – Reroute Jul 07 '18 at 06:10