Trying to filter one list according to whether elements are in another list in Python

I am trying to filter a list of genes I have obtained according to whether they are in a reference list. I have looked at these questions, which have been helpful, but they haven't helped me resolve the trouble I'm having (if/else in a list comprehension, List comprehension with else pass, if pass and if continue in python, Remove all the elements that occur in one list from another). Some of the answers from the last question in particular seemed very helpful but they didn't seem to work with my data.

I've tried to simplify what I'm doing, and this is a little toy example I have now:

head = genes_9.head()
diff_expressed_tf = [gene for gene in genes_9 if gene in head]
diff_expressed_tf

# This returns 
[]

I'm thinking that if I can get this to work with "genes_9.head()" it should work with my actual reference data.

Would someone be able to help me rewrite this to do what I want it to do? Alternatively, if someone could point me towards other relevant questions, I would also appreciate that greatly.

For reference, here is a little snippet of my data:

genes_9.head(10)


0      Tnfrsf4
2     Tnfrsf18
14       Il2ra
5         Odc1
7        Foxp3
36       Ctla4
3        Ikzf2
1          Cd5
8         Ccr8
24     Tnfrsf9

Solution 1:

If your datatype is a Pandas Series, then you can use 'iteritems()' instead of 'iterrows()' like this:

diff_expressed_tf = [gene for index, gene in genes_9.iteritems() if gene in head]

Solution 2:

Python lists don't have a "head" method. Instead, try genes_9[0:10] for the first 10 elements in the list.

If you're using a Pandas DataFrame, you could use the iterrows() method for iterating over the rows. For example:

diff_expressed_tf = [gene[gene_column] for index, gene in genes_9.iterrows() if gene in head] where gene_column is what column your gene is named in.

Otherwise, your code looks fine.