2

This one should not be too hard but, its giving me trouble:

Lets say I have two lists:

list1 = ['the sky is blue', 'I am happy', 'Where is the car', 'lets eat here']
list2 = ['the','', 'Where', 'lets']

What I am trying to do is compare list 1 and 2, and if the contents of list2 matches anything in list1, those characters will be removed from the member of the list.

for each new member, I would like it to be added to a new list that for now will be empty

list3 = []

list one can remain intact, but the important part is that list 3 should be the following:

list3 = ['sky is blue', 'I am happy', 'is the car', 'eat here']

Notice that the first word is removed in list 3 because that word matched when comparing list1 and list2. Hope this makes sense.

for laughs here what I tried...

list1 =  ['the sky is blue', 'I am happy', 'Where is the car', 'lets eat here']
list2 = ['the','', 'Where', 'lets']
list3 = []

for w in list2:
if w in list1:
    addthis = list1.remove(w)
    list3.append(addthis)

Thanks

Kevin
  • 391
  • 3
  • 6
  • 22
  • to make it clear, you want to perform the remove only on the corresponding list item, not on all items from `list2` for each item from `list1` right? – Jean-François Fabre Jan 18 '17 at 06:34
  • It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/questions/how-to-ask). – TigerhawkT3 Jan 18 '17 at 06:34
  • sorry bout that i figured my tries were so wrong they did not even warrant writing them down – Kevin Jan 18 '17 at 06:38
  • @TigerhawkT3 it's not exactly the same, OP seems to need to remove the corresponding substring, not the same substring for all items. But it's close enough. – Jean-François Fabre Jan 18 '17 at 06:43
  • Hi Jean-Francois, thanks for the response, but I was probably unclear in my question as I am not sure what you mean. I need list 3 to contain list1 but without the words that are members of list2 – Kevin Jan 18 '17 at 06:44
  • @Jean-FrançoisFabre - It's not a 100% identical situation, but I don't think the addition of "and use `zip`" warrants much attention. – TigerhawkT3 Jan 18 '17 at 06:46
  • 1
    @TigerhawkT3 agreed. Note that I did not reopen the question. – Jean-François Fabre Jan 18 '17 at 07:19

1 Answers1

2

use zip in a list comprehension to achieve this. strip the remaining spaces afterwards

list1 = ['the sky is blue', 'I am happy', 'Where is the car', 'lets eat here']
list2 = ['the','', 'Where', 'lets']

list3 = [x.replace(y,"").strip() for x,y in zip(list1,list2)]

print(list3)

result:

['sky is blue', 'I am happy', 'is the car', 'eat here']
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • o you responded down here thanks man ill try it out. I gotta learn this zip stuff! You the man! – Kevin Jan 18 '17 at 06:45
  • quick question, why did you add the "" in the replace method – Kevin Jan 18 '17 at 06:53
  • looks like its just a placeholder or something because I just took the "" out and the program crashed saying it needed two arguments. – Kevin Jan 18 '17 at 06:55
  • @Jean Another way to do it: http://stackoverflow.com/a/2400577/4082217 – Mohammad Yusuf Jan 18 '17 at 07:04
  • @Kevin - `""` is an empty string. Please read the documentation on these functions, as this site is not a tutorial service. – TigerhawkT3 Jan 18 '17 at 07:16
  • @Kevin: "I need list 3 to contain list1 but without the words that are members of list": that's not what my code does, and what you expect: it doesn't replace "the" in the 3rd item because "the" is in first replacement position. – Jean-François Fabre Jan 18 '17 at 07:20