Reputation: 11
My code compares a nested list of words and returns back whether the two words are anagrams of each other or not
Upvotes: 0
Views: 44
Reputation: 81594
You can use a list comprehension:
print(["anagrams" if sorted(words[0].lower()) == sorted(words[1].lower()) else
"not_anagrams" for words in nested_anagrams])
Or create a list
and append
to it if you think this is not readable:
output = []
for words in nested_anagrams:
if sorted(words[0].lower()) == sorted(words[1].lower()):
output.append("anagrams")
else:
output.append("not_anagrams")
print(output)
You can even remove the if
, but please don't do that in production code ;)
print([["not_anagrams", "anagrams"][sorted(words[0].lower()) == sorted(words[1].lower())]
for words in nested_anagrams])
Upvotes: 3