Skip to content

Comment on Python: copying a list the right wayparent

Comments

list.remove() returns None. So it is a mistake to bind its return value to allbut2.

It follows the convention that methods that modify their object inplace should return None. list.pop() is an obvious exception.

You create a new list via list comprehension instead of copying and then removing:

  even = [i for i in L if i % 2 == 0] # remove odd numbers

I believe he meant:

    all = range()
    allbut2 = all
    allbut2.remove(2)
which is something that catches people all the time.
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.