Compare two lists and replace value in third list if equal

Viewed 410

I am currently doing an online beginners course in Python and am stuck with this one element.

I have

listA = ['a','d','g','p','l','g','r','e']

listB = ['a','p','p','l','e']

listC = ['_','_','_','_','_']

I am comparing letters in list a to see if they are in list B, and if true then replace the same index in list C with that letter.The following code is in a function:

for a in listA:
        for b in listB:
            if a==b:
                listC.append(listB.index(b))
    stringC = str(listC)    
    print(stringC)  
    

The problem is that list C gets appended at the correct place, but with the int value of the index.

How can I replace the letter and not the index value?

Many Thanks!

6 Answers

You should use that :

listA=['a','d','g','p','l','g','r','e']

listB=['a','p','p','l','e']

listC=['','','','','_']

for a in range(len(listA)):
    letter = listA[a]
    if letter in listB:
        listC[listB.index(letter)] = letter
stringC = str(listC)
print(stringC)

It is faster, easier to understand, and works (even though I'm not totally sure I understood your question)..

Assuming I understand the question, you don't want to append to listC, but instead you want to modify elements of listC:

for a in listA:
    if a in listB:
        listC[listB.index(a)] = a

If you are searching trying to compare the location of the characters too

listA=['a','d','g','p','l','g','r','e']

listB=['a','p','p','l','e']

listC=['','','','','_']

minlen = min(len(listA), len(listB))
for elem in range(minlen):
    if listA[elem] == listB[elem]:
        listC[listB.index(listA[elem])] = listA[elem]
print("{}".format(listC))

If checking only if they are available answer by Wiseman looks good

You can do reverse search without the need of calling index, which makes it much faster (and specially if listA is longer than listB the search will be even shorter) and finally use list comprehension (which again is faster than loop):

listC = [x if x in listA else '_' for x in listB]

Also note that, this way handling duplicates is automatic and much easier.

output for your example:

listC
['a', 'p', 'p', 'l', 'e']

Normally this should do:

listA=['a','d','g','p','l','g','r','e']
listB=['a','p','p','l','e']
listC=['','','','','_']
 
for a in listA:
    for b in listB:
        if a==b:
                listC[listB.index(b)] = b
 
listC    
#Outputs
['a', 'p', '', 'l', 'e']

But if you instead want not to escape duplicate items in listC, then a bit of modification is required:

for a in listA:
    for b in listB:
        if a==b:
            if b in listC:
                listC[(listC.index(b)+1)] = b
            else:
                    listC[listB.index(b)] = b

Note that this will also fail if you have listB=['a','p','p','p','l','e']. Then better approach would be wrapping it into a recursive function.

> listA=['a','d','g','p','l','g','r','e']
> listB=['a','p','p','l','e']
> listC=['','','','','_']

> for index, value in enumerate(listB):
>     listC[index] = listB[index] if value in listA else listC[index]


> print(listC)
['a','p','p','l','e']
Related