'in <string>' requires string as left operand, not list

I am learning to develop an app that takes an input of words from a .txt file and checks if the words exist in the dictionary file.

When I run the code, I am getting a type error. Please help me with what I am doing here wrong.

The input.txt file has sentences of paragraphs. The dictionary.txt file has a list of words line by line.

def word_check(check_file, input_file):
    try:
        open_file = open(check_file, "r")
        read_file = open_file.readlines()

        open_file_2 = open(input_file, "r")
        read_file_2 = open_file_2.readlines()

        for input_word in read_file_2:
            input_word = input_word.strip("!@#$%^&*()_+{}:?><'-=,./;][")
            each_input_word = input_word.lower().split()
            list_each_word = each_input_word
            count = 0

            for item in read_file:
                line = item
                for word in line:
                    check_word = word.lower()
                    if list_each_word in check_word:
                        count += 1
                print(count)

    except FileExistsError:
        print("File not exist")


word_check("list.txt", "input.txt")

I would like to have a count of words if the words exist in the dictionary.txt file.

You generate list_each_word by doing input_word.lower().split().

This generates a list of words.

Then later on you loop trough a list of words and do if list_each_word in check_word:.

Here, check_word is a string, and list_each_word is a list. You should switch these around, because you want to check if the string is in the list.

So it should be:

if check_word in list_each_word :

If you're trying to count the occurrences of check_word in list_each_word, you can replace these two lines:

if list_each_word in check_word:
    count += 1

with this:

count += list_each_word.count(check_word)

which will find how many times check_word occurs in list_each_word.


    

I had figured out.

    def spell_check(dictionary, document):
    try:
    open_document = open(document, "r")
    input_sentence = open_document.readline()
    input_words = input_sentence.lower().split()
    print(input_words)

    open_dictionary = open(dictionary, "r")
    check_sentence = open_dictionary.read()
    check_word = check_sentence.lower()

    for word in input_words:
        word = word.strip("!@#$%^&*()_+{}:?><,./;[]=-")
        if word not in check_word:
            print(f"Mispelled words are: {word}")

    except FileExistsError:
    print("File does not exist")


    spell_check("dictionary.txt", "document.txt")

Comments
  • The line that reads if list_each_word in check_word: should read if check_word in list_each_word:. The error you're getting is saying the you need to check if a string is in a list, not if a list is in a string.
  • Please indicate what line of code the error is pointing to. That information should be included in the error message. I assume your title is the actual error message? If so, it is telling you exactly what is wrong: you have an ... in <string> expression that requires the left hand side of in to be a string, but you are not doing that. So it's an error.
  • Always remember to close the files you open.
  • @Ahndwoo When i do if check_word in list_each_word : that will run check the words in the dictionary file exist in the input file. I want to do the opposite. I would like to check if the words in the input file to be checked in the dictionary file. I am stuck at point on how to pass the list_each_word into strings word by word to be checked if the words exist in the dictionary file (check_file). Thank you!
  • @TeneshVignesan Okay, so your goal is to see how many times check_word appears in list_each_word?
  • When i do if check_word in list_each_word : that will run check the words in the dictionary file exist in the input file. I want to do the opposite. I would like to check if the words in the input file to be checked in the dictionary file. I am stuck at point on how to pass the list_each_word into strings word by word to be checked if the words exist in the dictionary file (check_file). Thank you!
  • I think i am confusing you. Firstly, I would like to spell check a paragraph from txt file. I would like to compare each word in the paragraph with a dictionary txt file. The paragraph file = input_file. The dictionary file = check_file. I would like to count only the words in the paragraphs that exists in the dictionary file. As for now i am stuck with on how to pass each words in the paragraph as a string word by words. But when i run the code, it passes alphabet by alphabets. I hope i made it clear. I am sorry for the inconvenience. @Ahndwoo
  • @TeneshVignesan Okay, well there's a difference between "I'm having this error", and "I want my code to do this and I'm not sure how". Your question asks about an error, which a few of us answered in the comments. This answer may not be what you're looking for, but now that you know what's causing the error, you can try to work around it. If you're still stuck, I would suggest asking a new question about that issue specifically.
  • i see. I am sorry that i am new to stackoverflow. i will try work around first and post new question. Thank you!
  • I had figured out the answer. Thank you! @Ahndwoo