About x+=2 and x = x + 2
I used to like the compactness of the expression, like using x += 2 instead of x = x + 2. But, now I am learning Hamiltonian Path, or Graph Theory. This is my graph dictionary: g = {'a':['c'], 'b':['c','e','f'], 'c':['a','b','d','e'], 'd':['c'], 'e':['b','c','f'], 'f':['b','e'] } According to the dict, each key is able to access to one of their element in the list. So it form a graph I like to find all paths which are from node 'a' to 'f'. In the class Graph: I define a method call 'find_all_paths' which takes a start_vertex, end_vertex, and a path in the parentheses. This is my method's code, and please pay attention to the third line which have += . def find_all_paths(self, start_vertex, end_vertex, path = []): graph = self.graph_dict path += [start_vertex] if start_vertex == end_vertex: return [path] if start_vertex not in graph: return [] paths = [] for vertex in graph[start_vertex]: if vertex not in path: extended_path = self.find_all_paths(vertex, end_vertex, path) for p in extended_path: paths.append(p) return paths Using this to find all paths from 'a' to 'f', I get [['a', 'c', 'b', 'e', 'f', 'd']] But now I test this but changing third line to path = path + [start_index] instead using +=: I get[['a', 'c', 'b', 'e', 'f'], ['a', 'c', 'b', 'f'], ['a', 'c', 'e', 'b', 'f'], ['a', 'c', 'e', 'f']] which is the expected answer. What wrong have I done? Is there any explanation about why += cannot form the same output?