Question #91189

Given a list of strings, return the count of the number of
strings where the string length is 2 or more and the first
and last chars of the string are the same.
Word1= ['aba', 'xyz', 'aa', 'x', 'bbb']
Word2 = ['', 'x', 'xy', 'xyx', 'xx']
Word3 = ['aaa', 'be', 'abc', 'hello']

Expert's answer

Create a function that as an argument gets a list of strings and returns the number of elements matching the conditions.

def count_string(arg_list):
    result = 0 
    for item in arg_list:
        if (len(item) >= 2 and item[0] == item[-1]):
            result += 1
    return result


Example of a function in a python interpreter:

Word1= ['aba', 'xyz', 'aa', 'x', 'bbb'] 
count_string(Word1)
3




LATEST TUTORIALS
APPROVED BY CLIENTS