6

I am working on a part of a program that turns a statement into a question.

When I try to remove x it returns None. I want it to print the sentence with that item removed, what am I doing wrong?

def Ask(Question):
    Auxiliary = ("will", "might", "would", "do", "were", "are", "did")
    for x in Auxiliary:
        if x in Question:
            Question_l = Question.lower()
            Question_tk_l = word_tokenize(Question)
            Aux_Rem = Question_tk_l.remove(x)
            print (Aux_Rem)

Example for behaviour wanted:

"what we are doing in the woods"

should become

"what we doing in the woods"

I want to remove any auxiliary word from question.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Sergei Glimis
  • 390
  • 2
  • 5
  • 17
  • 1
    By the way, don't modify lists while iterating on them, or you will get an exception – Andrea Corbellini Jul 12 '17 at 00:29
  • Do you want to remove _all_ instances, or just one at a time? – 101 Jul 12 '17 at 12:41
  • Does this answer your question? [Removing item from list causes the list to become nonetype](https://stackoverflow.com/questions/26766587/removing-item-from-list-causes-the-list-to-become-nonetype) – mkrieger1 Mar 13 '21 at 09:31

3 Answers3

4

That's correct behaviour. remove removes that element and doesn't return a value (i.e. returns None). You can use pop if you wish to access the removed element.

101
  • 8,514
  • 6
  • 43
  • 69
  • 2
    no i want to access the string now without that item in it. that's why i removed it shouldn't it print the Question without the Aux? – Sergei Glimis Jul 12 '17 at 00:30
2

somelist.remove(x) removes the first element it finds that equals x from somelist. It doesn't return the modified list. To print the modified list, simply print the list.

print(Question_tk_l)

If you want to turn it into a nice string, you should join it with space.

print(' '.join(Question_tk_l))
kichik
  • 33,220
  • 7
  • 94
  • 114
  • here i have this now def Ask(Question): Auxiliary = ("will","might","would","do","were","are","did") for x in Auxiliary: if x in Question: Question_l = Question.lower() Question_tk_l = word_tokenize(Question) Question_tk_l.remove(x) print(' '.join(Question_tk_l)) else: print ("thingy") – Sergei Glimis Jul 12 '17 at 01:02
  • and i got this error: Traceback (most recent call last): File "", line 1, in Ask("what you were doing") File "/media/pi/TRAVELDRIVE/Jarvis AI Code/Jarvis_Resourses/Jarvis_Functions.py", line 101, in Ask Question_tk_l.remove(x) ValueError: list.remove(x): x not in list – Sergei Glimis Jul 12 '17 at 01:03
  • As the error says, it's not in the list. You can't remove something that's not there. – kichik Jul 12 '17 at 01:15
  • Never Mind i got the results i wanted now but your answer definitely helped my get there thank you! – Sergei Glimis Jul 12 '17 at 01:52
0

You were very very nearly right. You dont need to create a new variable for the modified list. You can just do:

def Ask(Question): 
    Auxiliary = ("will", "might", "would", "do", "were", "are", "did") 
    for x in Auxiliary: 
        if x in Question:
            Question_l = Question.lower() 
            Question_tk_l = word_tokenize(Question) 
            Question_tk_l.remove(x) 
            print (Question_tk_l)

So I got rid of the (Aux_Rem) variable and just modified the list then reprinted the list.