0

My program uses this variable:

questionNum = random.randint(0,9)

Later in the program I tried to remove an element in the list by doing this:

questionList.remove([questionNum])

I got this error:

list.remove(x): x not in list

How can I remove an element in the list at the index questionNum?

2
  • 2
    list.remove(x) searches for values, not indexes. list.pop(index) would do what you want. It's in the documentation. Commented Dec 9, 2016 at 14:18
  • 2
    Just as an FYI, a Google search with exactly the title of your question yielded some very good answers to this question. Please try searching before asking. Commented Dec 9, 2016 at 14:27

1 Answer 1

2

list.remove takes the element to remove, not the index. list.pop is what you want.

Be aware, though, that this decreases your list's size. So, if you pop the element at index 5, elements at index 6, 7, 8 etc are now in index 5, 6, 7 etc.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.