user584694 user584694 - 3 months ago 10
Python Question

Get the number of same string in a list

I want to get the no. of same string in a list

Example

list = ['jack','jeen','jeen']
number_of_jeen = getnumber('jeen',list)
print(number_of_jeen)


Output

2


I have tried this so far

def getnumber(string_var,list):
if any(string_var in s for s in list):
print("This user exits !")

Answer

There's a built-in method count that does this.

number_of_jeen = list.count('jeen')