## dustbunny000

Say I have two lists:

list1=[a,b,c,d,e,f,a,b,c]
list2=[a,c,d]

How could I compare list1 and list2 such that I would get an output like:

list3=[match,-,match,match,-,-,-,-,-]
So that it goes in order
Here's my code:

``````h=[]
for i in range(len(change2)):
for j in range(len(atom)):
if atom[j]==atom[i]:
h.append("H")
else:
h.append("-")
print h``````

Both change2 and atom have letters with a particular order. atom has 479 letters and change2 has 188 letters. I want an output that is a list with 479 elements. I want to scan through atom until the first element is found in change2. I want the list to have elements "H" if they match and "-" if they do not. My code is giving me a huge list! I'm not sure how to make it ordered.

Thanks!

## woooee 814

Add a print statement so you know what is being compared, and hence if the append is correct or not. Also, you should first check that both lists are the same length.

``````h=[]
for i in range(len(change2)):
for j in range(len(atom)):
print "comparing" atom[j], atom[i]
if atom[j]==atom[i]:
h.append("H")
else:
h.append("-")
print h``````

## dustbunny000

Add a print statement so you know what is being compared, and hence if the append is correct or not. Also, you should first check that both lists are the same length.

``````h=[]
for i in range(len(change2)):
for j in range(len(atom)):
print "comparing" atom[j], atom[i]
if atom[j]==atom[i]:
h.append("H")
else:
h.append("-")
print h``````

Both lists are not the same length. I want to compare the first element in the list change2 to the elements in list atom until a match is found. I want the match to append "H" to a new list (the same length as atoms) and those that don't match append "-". Then go on to the next element in list change2.

## Gribouillis 1,391

I suggest

``````#python 2 and 3

change2 = list('abcdefabc')
atom = list('acd')

atomset = set(atom)
h = ['H' if x in atomset else '-' for x in change2]

assert(''.join(h) == 'H-HH--H-H')``````

## dustbunny000

I suggest

``````#python 2 and 3

change2 = list('abcdefabc')
atom = list('acd')

atomset = set(atom)
h = ['H' if x in atomset else '-' for x in change2]

assert(''.join(h) == 'H-HH--H-H')``````

That's not working either. What is the assert doing?

## pyTony 888

``````found = - 1
for search in list2:
if seach in list1[found+1:]:
found = list1.index(search)
#handle correct case
else: