Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Fix Bug #2: Search results
This is a somewhat subtle bug that hinges on the difference between `.append` and `+=` (most languages have equivalent methods that may be called something slightly different). `.append` adds the given argument as a new element to the list. The argument can be anything: ``` x = [] x.append(1) x.append(2) print(x) => [1, 2] x.append([3]) print(x) => [1, 2, [3]] ``` `+=` adds the given argument (which must be a list) to the original list. ``` x = [] x += [1] x += [2, 3] print(x) => [1, 2, 3] ``` Now we can see the difference between `.append` and `+=` in our program. When we pass in a list to `.append`, it adds that list as a single element, giving us a list-of-lists. This is not what we want: ``` search_results1 = ['banana-0', 'banana-1'] search_results2 = ['apple-0', 'apple-1'] x = [] x.append(search_results1) x.append(search_results2) print(x) => [['banana-0', 'banana-1'], ['apple-0', 'apple-1']] ``` However, `+=` concatenates the given list with the existing list. This is exactly what we want: ``` search_results1 = ['banana-0', 'banana-1'] search_results2 = ['apple-0', 'apple-1'] x = [] x += search_results1 x += search_results2 print(x) => ['banana-0', 'banana-1', 'apple-0', 'apple-1'] ```
- Loading branch information