Skip to content

Comment on Python: copying a list the right way

Comments

    b = a[:]
This happens to be fastest, and language recommended way to copy lists.

I really don't buy the argument about languages being intuitive to people who don't know the language. Languages should be optimized for people who know the language. I think Matz has mentioned it somewhere regarding "principle of least surprise" and Ruby.

Knowledge of a language is not a Boolean value. I use Ruby on daily basis, and occasionally read (and to a lesser extent write) Python, and I knew what "list(a)" does instantly, and I could only guess about "a[:]". Fortunately, both Python and Ruby has very few such gotchas, that's why I love them both.

AboutSource Built by g1lg1l

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