DO NOT replace [:] with list() next time you see it.<p>While the article explains some concepts well, [:] is no less pythonic than list(), though the latter might be more readable for people completely new to the language. In fact, it would be helpful to become very comfortable with python slices early on, because they allow you to easily manipulate any sequence, not just lists. For example, you can reverse a string using slices in one line:<p><pre><code> reversed_string = orig_string[::-1]
</code></pre>
Staying away from slices, you're leaving that power behind.
Huh, I've almost never seen [:], would never think of it. Do people really use that? I mean it takes a lot of work to make python cryptic but I guess if you're determined anything is possible.<p>use deepcopy or list().<p>I don't even use [] or {} to create empties anymore. I much prefer explicit esp since there is proliferation of container types and it's silly, inconsistant, and confusing that dicts and lists have syntactic exceptions.<p><pre><code> newlist = list()
newdict = dict()
newdict = defaultdict(str)
</code></pre>
etc.<p>And to anuraggoel. not using [:] is not avoiding slices. just as not using
string += "ext"
(esp in loop) is not avoiding strings.
Nice, clear explanation of something that drives Python beginners crazy--especially if it's their first programming language.<p>It would have been worth mentioning the 'copy' module.
'copy.copy' for shallow copies of <i>any</i> object,
'copy.deepcopy' for recursive copies.
<i>This is confusing for beginners and should be avoided.</i><p>That doesn't follow.<p>Beginners need to learn list slicing to get anywhere with Python, and by the time they've got through [1], [0:10], [2:], [:5], [:-1], [0:10:2] and so on then [:] is just another use in the same pattern.
Is this more immediately obvious? I would have expected list to be defined as list(*elements) and called like this list(1, 2, 3)<p>I guess that this allows me to type help(list) and figure it out, but I would have done that anyway to identify an operator.<p>Clearly the answer is a .clone() or .copy() method...