Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.

use deepcopy or list().

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.

    newlist = list()
    newdict = dict()
    newdict = defaultdict(str)
etc.

And to anuraggoel. not using [:] is not avoiding slices. just as not using string += "ext" (esp in loop) is not avoiding strings.



Creating empties with {} takes just 58% of the time as dict() [0.25 us, 0.39 us] because there's no need to LOAD_GLOBAL and CALL_FUNCTION.

'not not variable' is similarly faster than bool(variable).

(not that I've run into this often in Python.)


> takes just 58% of the time

Whoopee fucking doo, really.


I always used [:], although now I think I'll switch to copy.copy or list() after reading this thread. (Actually, now that I work for BigCo, I'll have to check what their coding standards say about this.)


I've also used list() because I find it just generally more readable.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: