Skip to content
Permalink
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
robert committed Dec 29, 2019
1 parent 8d5aa5f commit 0c2a12270960167dda364131ee34b4c7d338144b
Showing 1 changed file with 1 addition and 1 deletion.
@@ -2,7 +2,7 @@ def search_all(queries):
all_results = []
for q in queries:
results = search(q)
all_results.append(results)
all_results += results
return all_results

def search(query):

0 comments on commit 0c2a122

Please sign in to comment.