TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Python: copying a list the right way

71 pointsby henryprecheurover 16 years ago

7 comments

anuraggoelover 16 years ago
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.
评论 #479016 未加载
评论 #478949 未加载
评论 #479034 未加载
评论 #479198 未加载
njharmanover 16 years ago
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.
评论 #479614 未加载
评论 #479376 未加载
评论 #479569 未加载
mjtokellyover 16 years ago
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.
评论 #479138 未加载
评论 #478886 未加载
评论 #479020 未加载
yesimahumanover 16 years ago
I thought this was going to be a talk on performance issues. I think most python people dealing with production code would understand either.
jodrellblankover 16 years ago
<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.
评论 #479029 未加载
评论 #478900 未加载
评论 #479746 未加载
snprbob86over 16 years ago
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...
评论 #478987 未加载
评论 #478965 未加载
pkruminsover 16 years ago
&#60;moody&#62; "a[:] feels a bit too much like Perl" - doesn't feel like Perl at all!