4

For example i have 2 lists:

a = ['podcast', 'podcasts', 'history', 'gossip', 'finance', 'business', 'kids', 'motivation', 'news', 'investing']
b = ['podcast', 'history', 'gossip', 'finance', 'kids', 'motivation', 'investing']

I want to find items in list a that are not in the list b

I try to do it like this:

c = []
for _ in a:
    if _ not in b:
        c.append(_)

All that I try ends with something like this: image

Initially, I have a text file with keywords:

podcast
podcasts
history
gossip
finance

Also for almost all keywords I have text files with information:

podcast.txt
podcasts.txt
history.txt

I need to find which files I am missing I load a list of keywords like this:

a = []
with open("keywords.txt", "r") as f:
    text = f.read().split("\n")
    for k in text:
        a.append(k)
b = [e.replace(".txt", "") for e in os.listdir("profiles/")]
kshnkvn
  • 876
  • 2
  • 18
  • 31
  • `[i for i in a if i not in b]` – Onyambu Aug 26 '19 at 21:46
  • `c=[el for el in a if el not in b]`. Not worth answering – Pynchia Aug 26 '19 at 21:48
  • I mean...I'm just eye balling this, and I feel like this works. What's the problem? – Joshua Schlichting Aug 26 '19 at 21:49
  • @JoshuaSchlichting I do not know why this does not work for me – kshnkvn Aug 26 '19 at 21:51
  • @kshnkvn I just ran this, and it works. What error are you getting? – Joshua Schlichting Aug 26 '19 at 21:52
  • Looking at your other comments on other answers, it appears you have more code you're not showing us. I copy pasted your code and it leaves me with `c` as `['podcasts', 'business', 'news']`... – Joshua Schlichting Aug 26 '19 at 21:54
  • @JoshuaSchlichting updated a question – kshnkvn Aug 26 '19 at 21:59
  • @kshnkvn You're messing up the "flow" man! lol, This question "How to find list items that are not in another list?" has been answered before, so this was marked as a duplicate. Your best bet now is to delete this question, and make another post describing this issue at hand (not the duplicate list problem, your code solved that already!). – Joshua Schlichting Aug 26 '19 at 22:01
  • @JoshuaSchlichting yes, my question is how to find elements from one list which are not in another list and not one solution does not work for me. what is the problem? – kshnkvn Aug 26 '19 at 22:08
  • @kshnkvn I'm trying to help you. No one can answer your question *here* because it was marked as a duplicate. Your core question of "how to find items in a list that aren't in another" has already been answered on stack overflow, and your original code actually works for you. If you're still getting an empty list for `c`, that means you have more going on. You need to delete this question, and post another with more detail. There is more going on in your code that you don't realize, and that's okay, we'll help you! We just need all the code. I see you posted more, but it's already closed! gl! – Joshua Schlichting Aug 26 '19 at 22:12
  • Also, use a more descriptive question name on the next one, this same question name will get your question closed quick! – Joshua Schlichting Aug 26 '19 at 22:13

3 Answers3

8

You can use a list comprehension:

 c = [i for i in a if i not in b]
 print(c)
 ['podcasts', 'business', 'news']
Onyambu
  • 67,392
  • 3
  • 24
  • 53
7

try this:

c = (set(a) - set(b))
Mike
  • 1,471
  • 12
  • 17
2

Can you try using numpy?

import numpy as np
list1 = [.....]
list2 = [.....] 
diff = np.setdiff1d(list2,list1)
Harish
  • 565
  • 1
  • 12
  • 34