Srk
Srk

Reputation: 69

Compare a (single small list) with a (list of lists) in python

I am working with python 3.6. I have a list coordinates = [101758584, 101837149, 101844851] and another list of lists rows = [['1', '36933096', 'CSF3R', 'chr1:g.36933096T>C', 'Pathogenic', 'Tarceva\n'], ['2', '25463483', 'DNMT3A', 'chr2:g.25463483G>A', 'Pathogenic', 'Tarceva\n'], ['2', '25469502', 'DNMT3A', 'chr2:g.25469502C>T', 'risk factor', 'Iressa\n']].... and this list goes on. I want to check if the numbers present in coordinates are present in the rows list. So far what I have tried is -

coordinates = [101758584, 101837149, 101844851]
rows = [['1', '36933096', 'CSF3R', 'chr1:g.36933096T>C', 'Pathogenic', 'Tarceva\n'], ['2', '25463483', 'DNMT3A', 'chr2:g.25463483G>A', 'Pathogenic', 'Tarceva\n'], ['2', '25469502', 'DNMT3A', 'chr2:g.25469502C>T', 'risk factor', 'Iressa\n']]
    for e in rows:
            if e[0] in coordinates:
                chromo_final.append(e)

        print(chromo_final)

The output for this is an empty list. The second thing that I tried was -

chromo_final=[x for x in rows if x[0] in coordinates]
print(chromo_final)

Even this code gives an empty list. One example of the output is -

7   101755060   CUX1    chr7:g.101755060A>G Likely pathogenic   Cotellic

The pattern in the coordinates is present on the second position of the output. This output can be of many lines as my list of lists is huge. I would love to know so as to exactly where I am going wrong and also how can I go about this code to get the correct output.

Upvotes: 0

Views: 37

Answers (1)

akash karothiya
akash karothiya

Reputation: 5950

One simple method is to use list comprehension, One thing to point out is data types of coordinates and rows[1] are different, and also your coordinate is at index-1 so instead of x[0] use x[1]

>>> [i for i in rows for j in coordinates if i[1] == str(j)]

Upvotes: 1

Related Questions