I'm a newbie in the Python world. I'm still struggling with some of the basic concepts of Python.
Question: **Write a program that counts the number of string from a given list of strings. This string is 2 characters or more and the first and last characters are the same **
As the question mentioned in the title, this was my approach:
def string_num(items):
for a in items:
len(a) >= 3 and (a[0] == a[-1])
return items.count(a)
print(string_num(['abc','xyz','aba','1221']))
I assumed that the .count()
function cannot be used here to count string in a list under a specific condition. I have been looking around but I still have not seen any explanation. Could someone help me explain what's wrong with this code, or what the .count()
function does exactly that it cannot be used here?
Appreciate all the help!