Search code examples
python-3.xcountcase-folding

Python looking to find an easier way to shorten my .casefold().count()


My python code is running fine but the code looks a bit tedious and messy. I was wondering if there was an easier way to write it. I have a text file and I am required to find if the letters 'aardvark' can be found inside the line.

if i.casefold().count('a') >= 3 and i.casefold().count('r') >= 2 and i.casefold().count('d') >= 1 and i.casefold().count('v') >= 1 and i.casefold().count('k') >=1:

Solution

  • if all(
     i.casefold().count(letter) >= 'aardvark'.count(letter)
     for letter in 'aardvark')
    

    kinda a silly solution but it works