-1

I'm trying to write a simple program that just runs through a list of strings and compares each one to a shorter list of strings that contain some of the same words. I then want to print out all of the words in the long list that aren't in the shorter list. I think I have the right logic but I cant seem to get the printing to work. Here is what I have:

oneList = ['egg', 'duck', 'cow']
twoList = ['egg', 'giraffe', 'cow', 'poo', 'speaker']

for twoString in twoList:
    for oneString in oneList:
        if (twoList[twoTicker] = oneList[oneTicker]):
            #do nothing
        else:
            #do nothing
    #if it reaches end of list and isnt there, print word.
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158

1 Answers1

2
  • This answers the question as it was written.
    • runs through a list of strings and compares each one to a shorter list of strings that contain some of the same words
    • print out all of the words in the long list that aren't in the shorter list

Use Membership test operations

  • Use not in
    • in checks a value for membership in another value.
    • In this case, if a value in b is not in a, print it.
a = ['egg', 'duck', 'cow']
b = ['egg', 'giraffe', 'cow', 'poo', 'speaker']

for v in b:
    if v not in a:
        print(v)


giraffe
poo
speaker

Using a list-comprehension

result = [v for v in b if v not in a]

print(result)

['giraffe', 'poo', 'speaker']

Using set

set(b) - set(a)

{'giraffe', 'poo', 'speaker'}
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158