1

I have two python lists generated from two different database tables

list1= ["'HZ1398043','HZ1395693','HZ1395532','HZ1395240','HZ1395194','HZ1395113','HZ1395036','HZ1395236','HZ1396139','HZ1398028','HZ1395098','HZ1395998','HZ1395018','HZ1395829','HZ1398031','HZ1395708','HZ1398029','HZ1398030','HZ1398054''"]

list2= ['', '', '', '', '', 'HZ1395018', 'HZ1395036', 'HZ1395098', 'HZ1395113', 'HZ1395194', 'HZ1395236', 'HZ1395240', 'HZ1395532', 'HZ1395693', 'HZ1395708', 'HZ1395829', 'HZ1395998', 'HZ1396139', 'HZ1398028', 'HZ1398029', 'HZ1398031', 'HZ1398043', 'HZ1397932', 'HZ1397949', 'HZ1398004', 'HZ1398021', 'HZ1398030', 'HZ1397940', 'HZ1397941', 'HZ1398010',', '', '']

I need to find common elements between the two

set(list1) & set(list2) 

doesn't display anything

even [i for i in list1 if i in list2] doesn't display anything. I can clearly see HZ1398043 is common.

1

4 Answers 4

8

Look closely, your first list is actually a list with one item, a big string.

>>> list1= ["'HZ1398043','HZ1395693','HZ1395532','HZ1395240','HZ1395194','HZ1395113','HZ1395036','HZ1395236','HZ1396139','HZ1398028','HZ1395098','HZ1395998','HZ1395018','HZ1395829','HZ1398031','HZ1395708','HZ1398029','HZ1398030','HZ1398054''"]
>>> len(list1)
1

Ideally, fix wherever you are getting the data to give you the right thing, if that's not possible, then you will need to parse the data.

You will want to do something like list1 = [item.strip("'") for item in list1[0].split(",")] to get the actual list (a simple list comprehension), then use one of your methods (the set method is the most efficient, although if you wish the keep duplicates and order, you will need to do the second method, although you can improve it by making a set of list2 beforehand to check for membership in).

Sign up to request clarification or add additional context in comments.

2 Comments

Oh yaa.. how do I split them into multiple items
I was editing the question, and came to know the answer that list1 actually contains a single element and its a string. I came back quickly and you pointed out already :)
2

First you need to make a proper list out of list1, this can be done with something like:

list1 = [item.strip("'") for item in list1[0].split(",")]

Then your code should work just fine. An alternative (more compact but slower) way to find common elements would be:

common = filter(lambda x:x in list1,list2)

1 Comment

o_O not quite sure what is wrong with this answer...thx for comments I guess.
1

This is what they should be:

list1= ['HZ1398043','HZ1395693','HZ1395532','HZ1395240','HZ1395194','HZ1395113','HZ1395036','HZ1395236','HZ1396139','HZ1398028','HZ1395098','HZ1395998','HZ1395018','HZ1395829','HZ1398031','HZ1395708','HZ1398029','HZ1398030','HZ1398054']
list2= ['', '', '', '', '', 'HZ1395018', 'HZ1395036', 'HZ1395098', 'HZ1395113', 'HZ1395194', 'HZ1395236', 'HZ1395240', 'HZ1395532', 'HZ1395693', 'HZ1395708', 'HZ1395829', 'HZ1395998', 'HZ1396139', 'HZ1398028', 'HZ1398029', 'HZ1398031', 'HZ1398043', 'HZ1397932', 'HZ1397949', 'HZ1398004', 'HZ1398021', 'HZ1398030', 'HZ1397940', 'HZ1397941', 'HZ1398010','', '', '']

Your code works after you fix them.

Comments

1
def compare(list1,list2):
ln= []
for i in list1:
    if i  in list2:
       ln.append(i)
return ln

print(compare(list1,list2))

not optimise but easy to understand.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.