-3

(This is just an example of what I'd like to be solved)

array = ["hello", "hi"]

statement = input()

condition = any(statement in elm for elm in array)

Is there a way to return the index of the element that returns True, or should I just use a for loop?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135

2 Answers2

0

In fact you want the index of the statement in array

array = ["hello", "hi"]
statement = input("Give a word: ")
condition = array.index(statement) if statement in array else -1
print(condition)

Demo

Give a word: hello
0
Give a word: hi
1
Give a word: Hi
-1
azro
  • 53,056
  • 7
  • 34
  • 70
0

Next time search the function properties. I write the example code base on Python documents

array = ["hello", "hi", "example", "to get", "index from array"]

def SearchIndex():
    statement = input()
    #// Check if input is item on list
    if statement in array: print(">>", array.index(statement))

    #// Other search if input is part of item in list
    else:
        part_of = False
        for item in array:
            if statement in item:
                print('>> {0} is part of "{2}" -> {1}'.format(statement, array.index(item), array[array.index(item)]))
                #// If founded signal
                part_of = True
        #// If nout found (signal is false)
        if part_of == False: print(f">> {statement} is not on list...")

while 1:
    SearchIndex()
ASI
  • 334
  • 3
  • 15
  • I know how to get the index using a for loop, I asked if there was a way to get the index without using a for loop, thanks anyway! – LittleNooblet Jul 11 '20 at 10:53
  • @LittleNooblet `array.index(statement)` is not a loop. I call it after **if** statement to avoid an error. What is after **else:** was just an example of how to get the index in case just one word was defined. `array.index(statement)` will return **int()** if will be a perfect match between _statemen_ and _array item_ – ASI Jul 11 '20 at 11:09