I want to define a function find_anagrams_in_word_list(str,str_list) where the parameters should be a string and a list of strings. The value returned should be a list of all the strings in the str_list that are values of str.
I have a simple code that allows you to see if a word is an anagram of a another
def anagram(str1,str2):
str1 = sorted(str1)
str2 = sorted(str2)
if str1 == str2:
print("True")
else:
print("False")
But i can't seem to understand how to use this for a list.
edit
def anagram(str1,str2):
str1 = sorted(str1)
str2 = sorted(str2)
if str1 == str2:
print("True")
else:
print("False")
def find_anagrams_in_word_list(str1,str_list):
anagrams = []
for word in str_list:
if word == str1:
if (anagram(str1,word)):
anagrams.append(word)
print(anagrams)
I have gotten this far in creating this code but instead of printing the words if they are anagrams of 'str1' it prints '[]' which i think means that the words in str_list are not being appended to 'anagrams'
I want a output like this
find_anagrams_in_word_list("rat", "[art,tar,gar,jay]")
'art' 'tar'
as they are anagrams of rat.
if anagram(word,str1)
checks each or in the list against the passed in str1, ifanagram
returnsTrue
then we keep that word, finally" ".join
outputs all the words in the list as a single string.