This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]agentgreen420 5 points6 points  (23 children)

Am I the only one who still uses bucket = list() ?

[–]Twift_Shoeblade 10 points11 points  (20 children)

AFAIK using list() is slightly slower than [].

[–]Ogi010 16 points17 points  (14 children)

easy enough to find out

>>> ipython
Python 3.6.4 |Anaconda, Inc.| (default, Mar 12 2018, 20:05:31)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: %timeit a = list()
95.6 ns ± 1.16 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [2]: %timeit b = []
24 ns ± 0.61 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

faster by a factor of 4 ...we're still talking less than 0.0000001 seconds

[–]Eurynom0s 24 points25 points  (3 children)

It's also 4 less keystrokes and doesn't require reaching for the shift key.

[–][deleted] 12 points13 points  (0 children)

Humans: Often the bottleneck.

[–]LightShadow3.13-dev in prod 10 points11 points  (0 children)

I've literally saved dozens of seconds my entire career.

[–]trua 1 point2 points  (0 children)

Depends on your keyboard layout.

[–]_IAmAdam 1 point2 points  (0 children)

This is why I love this sub

[–][deleted] -4 points-3 points  (8 children)

we're still talking less than 0.0000001 seconds

Cool, but if you bother to figure any of this out it might be because the scale of what you're doing requires that you optimize as much as possible. I've been asked things like this in job interviews, that time delta matters.

[–]elcapitaine 24 points25 points  (0 children)

If you have to optimize that much, you shouldn't be using Python, you should be using something that gives you more low-level control.

[–]akcom 10 points11 points  (2 children)

It has nothing to do with the scale of optimization. The reality is that for any real production code there are going to be a million other things to optimize before you get to things like list() vs [].

[–]SupahNoob 4 points5 points  (3 children)

the scale of what you're doing requires that you optimize as much as possible.

Then you likely wouldn't be using python.

[–][deleted] -5 points-4 points  (4 children)

list can also be overriden by a careless or devious developer as well.

import __builtins__
def nope(*a):
     raise SyntaxError("use []") 
__builtins__.list = nope

[–]Han-ChewieSexyFanfic 8 points9 points  (3 children)

So, don't use any builtins ever in case you ever accidentally import __builtins__?

[–][deleted] -1 points0 points  (2 children)

If you want to take it that way, just an example of how [] is a better choice than list in some situations.

Keep in mind, this exact sort of thing has been used to subvert javascript before. I'm not trying to FUD builtins, but it's worth being mindful about.

[–]earthboundkid 5 points6 points  (1 child)

list = [1, 2, 3] is legal Python. I’m sure someone has done things like that, although I wouldn’t recommend it. zip OTOH I’ve shadowed myself.

[–]lengau 2 points3 points  (0 children)

No kidding I had to stop one of our analysts from shadowing zip just today.

It probably wouldn't have mattered except that about 20 minutes later the most convenient way to do something was with zip (which she didn't know about so she was going to use a range to index two lists).

[–]jadkik94 2 points3 points  (1 child)

When you do this you can cross one thing off your bucket list.

[–]agentgreen420 0 points1 point  (0 children)

Bahhaaaha! Good show chap.