Python – how to change index value in python lists

listpython

I am trying to check the elements of two lists in python to see if they contain the same values in the same position (indexes) of them. If an element let's say in position 0 of list A is not the same with the element in position 0 of list B I want to extract that value from list A and start again the comparison.

My program is below:

 listA = ["book","2","stage","me","you"]
 listB = ["stage","me","you"]

listB is always a sublist of listA!!

  diff_list = []
  for n in range(0, len(listA)):
     for k in range(0, len(listB)):
         if n == k:
            if listA[n] != listB[k]:
                 rm_item = listA.pop(n)
                 diff_list.append(rm_item)
                 k==0
                 print(k)

In my terminal first k = 0 then k = 1. Is there a way to remove the item from listA and then start the comparison again?

Thanks for the help guys! To be honest….What I wish to do is to get the difference between two strings. I have two texts where text B is always subtext of text A. So I used splitlines() to split the two texts and then I want compare the two lists to get what I want!
Sorry but I am new to python and still can't figure out how a lot of things are done!

So I have

   textA='The first paragraph of the book is written well' 

and

   textB = 'the book is written well'

the result should be

  text_diff ='the first paragraph of' 

Best Solution

It seems to me that if you have two strings stringA and stringB (presumably lines from a larger text split on \n) such that stringB is always a substring of stringA, and you want to find the extra stuff in stringA, then it'd be easiest to do something like

stringA = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus aliquam leo odio, ut posuere dui rutrum ac. Nulla facilisi."
stringB = "ut posuere dui rutrum ac"

print stringA.replace(stringB, '')
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus aliquam leo odio, . Nulla facilisi."

Edit: Using your example text from the updated OP, you could do

textA = 'The first paragraph of the book is written well' 
textB = 'the book is written well'
text_diff = textA.lower().replace(textB.lower(), '')

which yields

'the first paragraph of'