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.

girasquid
  • 15,121
  • 2
  • 48
  • 58
Ank
  • 6,040
  • 22
  • 67
  • 100
  • For the question in title: [python - Common elements comparison between 2 lists - Stack Overflow](https://stackoverflow.com/questions/2864842/common-elements-comparison-between-2-lists?noredirect=1&lq=1) – user202729 Aug 15 '21 at 03:25

4 Answers4

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).

Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
  • Oh yaa.. how do I split them into multiple items – Ank Dec 19 '12 at 23:11
  • 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 :) – Aamir Rind Dec 19 '12 at 23:14
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)
j-i-l
  • 10,281
  • 3
  • 53
  • 70
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.

jackcogdill
  • 4,900
  • 3
  • 30
  • 48
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.

Mikhail
  • 2,690
  • 4
  • 28
  • 43