Someone rectify the bug in pythom | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Someone rectify the bug in pythom

How do u solve the value error. list.remove(x): x not in list https://code.sololearn.com/ckxA2JdHvFiX/?ref=app

1st Oct 2019, 5:45 PM
Thashreef Mohammed
Thashreef Mohammed - avatar
7 Answers
+ 3
pop(x) removes the element at index x. You can use also pop(-1) to remove the last element. pop() also returns the element it is removing so you can use it (maybe by assigning it to a variable) like; your_list = [1,2,3,4,5] last_element = your_list.pop(-1) print(your_list) #[1,2,3,4] print(last_element) #5
1st Oct 2019, 6:26 PM
Russ
Russ - avatar
+ 2
If you're trying to remove the last element, simply change 'remove' to 'pop'.
1st Oct 2019, 6:16 PM
Russ
Russ - avatar
+ 2
Oh cool! Tysm
1st Oct 2019, 6:28 PM
Thashreef Mohammed
Thashreef Mohammed - avatar
+ 2
No worries. Happy coding!
1st Oct 2019, 6:30 PM
Russ
Russ - avatar
0
What's the syntax of pop? And yeah im trying to remove last element
1st Oct 2019, 6:22 PM
Thashreef Mohammed
Thashreef Mohammed - avatar
0
I'm almost done making the code dynamic though!
1st Oct 2019, 6:50 PM
Thashreef Mohammed
Thashreef Mohammed - avatar
0
How can you remove the element which is not in the list! Therefore this error occurs. You will first have to append the element in the list using list.append(x) Then if you wish to remove a particular element from the list then that element needs to be there in the list.
2nd Oct 2019, 11:50 AM
namrata garg
namrata garg - avatar