Q:

Stuck on iterating through a list and returning True when a string ends with another string from the list

I receive a set of strings. I convert that to a list. I need to return a value of True if any of the strings in the list are a suffix of any other string in the list. For example: 

test = ['hello', 'lo', 'he']

This should return True since 'hello' ends with 'lo'. 
I tried this:
for s in words_list:
    if s.endswith(s, 0, len(words_list)):
       return True 
    else:
       return False

This returns 3 different values on the test list above but I only need a single value returned that reports True if any of the strings are a suffix of the other strings and False if none of them are. How could I best accomplish this?
Thanks!

A:

You need to compare every item in the list with every other item in the list, except for itself (since that would produce a false-positive -- it would always be true, since any string is technically a suffix of itself).
So, with that in mind, something like the following should work. I tested it against three different input lists, including one that shouldn't match at all (and doesn't):
#!/usr/bin/env python

words_list = ['hello', 'lo', 'he']
words_list2 = ['narf', 'poit', 'troz']
words_list3 = ['narf', 'narfpoit', 'troznarf', 'zort']

def check_words(words_list):
    print "#####"

    for outer_num, outer_word in enumerate(words_list):
        print "Outer word #{0}: {1}".format(outer_num, outer_word)

        for inner_num, inner_word in enumerate(words_list):
            print "Inner word #{0}: {1}".format(inner_num, inner_word)

            if outer_num != inner_num:
                if outer_word.endswith(inner_word):
                    print "True"
                    return True
                else:
                    print "False"

    return False

result = check_words(words_list)
print "result: {0}".format(result)

result = check_words(words_list2)
print "result: {0}".format(result)

result = check_words(words_list3)
print "result: {0}".format(result)

This produces the following output:
#####
Outer word #0: hello
Inner word #0: hello
Inner word #1: lo
True
result: True
#####
Outer word #0: narf
Inner word #0: narf
Inner word #1: poit
False
Inner word #2: troz
False
Outer word #1: poit
Inner word #0: narf
False
Inner word #1: poit
Inner word #2: troz
False
Outer word #2: troz
Inner word #0: narf
False
Inner word #1: poit
False
Inner word #2: troz
result: False
#####
Outer word #0: narf
Inner word #0: narf
Inner word #1: narfpoit
False
Inner word #2: troznarf
False
Inner word #3: zort
False
Outer word #1: narfpoit
Inner word #0: narf
False
Inner word #1: narfpoit
Inner word #2: troznarf
False
Inner word #3: zort
False
Outer word #2: troznarf
Inner word #0: narf
True
result: True

Note: Extra print statements are just for illustrative purposes - those can be commented out.

