0
my_list = ["banana", "apple", "cherry"]

var = my_list.remove("apple")

print(var)

When I run this it is suppose to write

apple

p.s I am new to coding

  • 1
    `list.remove()` does not return the removed element - why would it need to, since you already know which element it removed? You could use `var = my_list.pop(my_list.index("apple"))`, but that's extra steps. – Green Cloak Guy Aug 29 '20 at 02:07
  • Why did you expect `remove()` to return the removed element? – John Gordon Aug 29 '20 at 02:29

4 Answers4

0

In python, remove method for list does not return anything. It will throw an exception if the element does not exist. Not sure what you are trying to accomplish though.

srv236
  • 509
  • 3
  • 5
0

You can use pop instead. You have to specify the index you are removing from.

my_list = ["banana", "apple", "cherry"]

var = my_list.pop(my_list.index("apple"))

print(var)
Kral
  • 189
  • 1
  • 10
0

This is one of the reasons I always recommend using an IDE when programming in Python. PyCharm gives you the warning "remove doesn't return a value".

Frank Yellin
  • 9,127
  • 1
  • 12
  • 22
0

You can get element from list by slicing or pop() item from list

my_list = ["banana", "apple", "cherry"]
print(my_list[my_list.index('apple')])
#apple
print(my_list.pop(my_list.index('apple')))
#apple
Krishna Singhal
  • 631
  • 6
  • 9