Ask Your Question
1

Why does .remove() seem to take every element out of a list?

asked 5 years ago

sum8tion gravatar image

I've found that if I define a list

Sage: list1 = [1,2,3]

and then try to remove an element

Sage: list2 = list1.remove(2)

but then

Sage: print(list2)

returns

None
Preview: (hide)

1 Answer

Sort by » oldest newest most voted
4

answered 5 years ago

When you evaluate list1.remove(2), it modifies list1 and always returns None. It does not return the modified list. So:

sage: list1 = [1,2,3]
sage: list1.remove(2)
sage: list1
[1, 3]

Note that there is no output from the second line: it returns nothing. This in contrast to something like the sorted function which does not modify the original list but instead returns a new sorted list:

sage: list1 = [3,2,1]
sage: sorted(list1)
[1, 2, 3]
sage: list1
[3, 2, 1]
Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 5 years ago

Seen: 3,039 times

Last updated: Jul 20 '19