{"record":{"id":"0e6824e773008640","repo":"donnemartin/interactive-coding-challenges","slug":"items-cannot-be-none","errorCode":null,"errorMessage":"items cannot be None","messagePattern":"items cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sorting_searching/anagrams/anagrams_solution.ipynb","lineNumber":111,"sourceCode":"   \"metadata\": {},\n   \"source\": [\n    \"## Code\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 1,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"from collections import OrderedDict\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"class Anagram(object):\\n\",\n    \"\\n\",\n    \"    def group_anagrams(self, items):\\n\",\n    \"        if items is None:\\n\",\n    \"            raise TypeError('items cannot be None')\\n\",\n    \"        if not items:\\n\",\n    \"            return items\\n\",\n    \"        anagram_map = OrderedDict()\\n\",\n    \"        for item in items:\\n\",\n    \"            # Use a tuple, which is hashable and\\n\",\n    \"            # serves as the key in anagram_map\\n\",\n    \"            sorted_chars = tuple(sorted(item))\\n\",\n    \"            if sorted_chars in anagram_map:\\n\",\n    \"                anagram_map[sorted_chars].append(item)\\n\",\n    \"            else:\\n\",\n    \"                anagram_map[sorted_chars] = [item]\\n\",\n    \"        result = []\\n\",\n    \"        for value in anagram_map.values():\\n\",\n    \"            result.extend(value)\\n\",\n    \"        return result\"\n   ]\n  },\n  {","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/sorting_searching/anagrams/anagrams_solution.ipynb#L93-L129","documentation":"Raised by Anagram.group_anagrams when items is None. The method iterates items and builds an OrderedDict keyed by sorted character tuples, so a None list is rejected up front with a TypeError instead of failing inside the for loop.","triggerScenarios":"Calling group_anagrams(None). An empty list is valid and returned as-is; only None raises.","commonSituations":"Word lists loaded from a file or API where the words field is missing; a list comprehension variable that stayed None; chaining after a function that returns None on failure.","solutions":["Default to an empty list: items = items or []","Fix loaders to return [] instead of None on missing data","Check the source collection before calling"],"exampleFix":"// before\ngroups = anagram.group_anagrams(load_words(path))\n// after\ngroups = anagram.group_anagrams(load_words(path) or [])","handlingStrategy":"validation","validationCode":"items = items or []\nanagram.group_anagrams(items)","typeGuard":"def is_str_list(x):\n    return isinstance(x, list) and all(isinstance(i, str) for i in x)","tryCatchPattern":"try:\n    anagram.group_anagrams(words)\nexcept TypeError:\n    groups = {}","preventionTips":["Loaders return [] for missing word lists","Avoid None-returning chain steps"],"tags":["python","anagram","hashing","input-validation"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}