# Optimizing a list comprehension

My givens are:

• a list of N rna sequences (in string form) W = [W0, W1, W2, …, W(n-1)]

• a list of M queries, Q = [(i, j), (i1, j1), (i2, j2), …, (iN, jN)] (each 0 <= i & each j < N = len(W))

• if W_i & W_j have different lengths, assume W_i < W_j

Prompt: write a function less_than(W, Q) that returns whether W_i is less than W_j in alphabetical order, for each query

currently, my code is:

``````def less_than(W, Q):
return [W[i] < W[j] for i, j in Q]
``````

the function calls I have tried with my jupyter notebook so far include:

``````less_than(
["A", "AA", "AC", "UUG", "AUG", "CGT"],
[(0, 1), (1, 2), (2, 0), (3,4), (5,1), (4, 5), (2,4)],
)
``````

this returned:

``````[True, True, False, False, False, True, True]
``````

with a time of 946 ns ± 14.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

I have tried searching up ways to further optimize the code, such as seeing if maps or filter would optimize time complexity, but since I still need iteration and alphabetical comparison, I am not sure how I can make the code more efficient.

Hi Mai, and welcome!

We’re happy to help, but:

Please show us the code as text, together with any errors you got. Not as a screenshot or photo. We don’t use Photoshop to edit our code.

It will also help to show so sample data, say, with N and M around 6 or 8.

So I don’t have any code errors right now, since the code works but I want to optimize it, but I will upload the code from my jupyter notebook. I’m not sure how to show the results from the jupyter notebook run without a screenshot.

I just updated it with some sample data and the time given for the run by using %%timeit

I’m not a Jupyter expert, but I would expect that selecting the text with your mouse and copying will work.

If it doesn’t, that would surprise and horrify me. Copy and paste is basic functionality, and it would be shocking if Jupyter didn’t support it.

https://www.startpage.com/sp/search?query=copy+text+from+jupyter+notebook

suggests that maybe it is harder than it should be.

Alternatively, you could try attaching your notebook, although I don’t know if that is permitted by the Discuss software.

``````def less_than(W, Q):