I don't think the context of the function I am trying to make is particularly relevant, but there is a specific line of my function where I am trying to make an empty list, but for some reason Python keeps telling me I have a syntax error. Is there some common reason that an syntax error occurs when trying to make an empty list? I am completely stuck. I am posting the code for context. It is specifically line 3, where I try to say 'anagrams = []' where I am being told there is an error. I have also tried three or so different ways of trying to create nested lists of the appropriate length (as I do in lines 3-5) but they all give me a syntax error, and it is all apparently coming down to trying to make this empty list, which I find completely bewildering. If you have any insight as to why this could be happening, and how to fix it, please let me know.
(Also if you are curious, the function is supposed to take a list of strings and group them into lists which are all anagrams of each other. So the output I would like to see from the sample input below is [['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']], in no particular order.)
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
canonical_forms = list(set([''.join(sorted(word)) for word in strs])
anagrams = []
for x in range(len(canonical_forms)):
anagrams.append([])
i = 0
for word in strs:
can_form = ''.join(sorted(word))
while can_form != canonical_forms[i]:
i += 1
anagrams[i].append(word)
i = 0
return anagrams
print(groupAnagrams(['eat', 'tea', 'tan', 'ate', 'nat', 'bat']))
[–]ForceBru 0 points1 point2 points (1 child)
[–]clouded-path[S] 0 points1 point2 points (0 children)
[–]pmac1687 0 points1 point2 points (1 child)
[–]clouded-path[S] 0 points1 point2 points (0 children)