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
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
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.
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)
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".
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