Question: I have the following code:

```
ids = set()
for result in text_results:
    ids.add(str(result[5]))
for result in doc_results:
    ids.add(str(result[4]))

```

Both `text_results` and `doc_results` are lists that contain other lists as items as you might have already guessed. Is there a more efficient way to do this using a nifty oneliner rather than two for loops?

Answer: 
I would probably write:

```
ids = set(str(result[5]) for result in text_results)
ids.update(str(result[4]) for result in doc_results)

```

As for efficiency, if you want to squeeze every possible bit of performance then you first need a realistic dataset, then you can try things like `map` (or `itertools.imap` in Python 2) and `operator.itemgetter`, to see what's faster.

If you absolutely must have a one-liner:

```
ids = set(itertools.chain((str(result[5]) for result in text_results), (str(result[4]) for result in doc_results)))

```

Although, if you want a one-liner it's also worth optimizing for conciseness so that your one-liner will be readable, and then seeing whether performance is adequate:

```
ids = set([str(x[5]) for x in text_results] + [str(x[4]) for x in doc_results]))

```

This "feels" inefficient because it concatenates two lists, which shouldn't be necessary. But that doesn't mean it really is inefficient for your data, so its worth including in your tests.
This one liner should work:

```
ids = set(map (lambda x: str(x[4]), doc_results) + map(lambda x: str(x[5]), text_results))

```