Ask Your Question
1

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

asked 2019-07-19 20:56:46 -0500

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
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2019-07-19 22:32:18 -0500

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]
edit flag offensive delete link more

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: 2019-07-19 20:56:46 -0500

Seen: 36 times

Last updated: Jul 19