why
range(0,10).remove(1)
does not work?
I know the question is pretty short, but I do not have any idea why this thing does not work.. Is it possibe to write it in one line?
x = range(0, 10) will return either a list or a range object (depending on your Python version).
x = range(0, 10).remove(1) will return None as list.remove will modify the list in-place and so returns None. Your list is created and the value 1 removed, but as it is never assigned to anything, it's garbage collected.
x = range(0, 10).remove(1) will return an AttributeError as range objects don't have a remove method. If you convert it to a list using list(range(0, 10)).remove(1) you'll have the same issue as Python 2 though.
A way to get what you want would be to use a list comprehension. In the list comprehension you'll iterate over range(0, 10) but use an if statement to only add a value that isn't 1, such as:
x = [i for i in range(0, 10) if i != 1]
# [0, 2, 3, 4, 5, 6, 7, 8, 9]
Because the remove() method operates "in place", and doesn't return anything.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With