I am trying to write a Python program that counts the number of strings where the string length is 2 or more and the first and last character are same from a given list of strings.
Sample List : ['abc', 'xyz', 'aba', '1221'] - expected answer is 2
I have the code using a function, but am curious to know whether there is a simpler way to write it by using list comprehension.
I wrote the following code but it doesn't work. Is it because the question is unsolvable with list comprehension or because I have gotten something wrong with the code?
li=['abc', 'xyz', 'aba', '1221']
li.count(x for x in li if len(x)>1 and x[0] == x[-1])
[x for x in li if len(x)>1 and x[0] == x[-1]]should work.len([x for x in li if len(x) >= 2 and x[0] == x[-1]])