0
Check if something from a list appears twice in a string
I have a code that checks if something from a list is in string: if any(x in string for x in list) But how do I check if it appears twice or more than twice
8 Réponses
+ 6
any(string.count(x)>=2 for x in lst)
Will be true if at least 1 item in lst appears twice or more in string.
+ 5
Kirill Vidov Something like this:
s = 'Sololearn'
l = ['a', 'o', 'l', 'r', 'n']
print([x for x in l if s.count(x) >= 2])
# will only print 'o' and 'l'
+ 2
Thank you everyone for the help
+ 1
Kirill Vidov Show ur attempt
+ 1
Kuba Siekierzyński can you please help if you can?
+ 1
Exactly, I would use count() to determine if there is more than one occurence of an element in a collection.
0
Abhay if any(x in string for x in list) >= 2, but that doesn't work