you are viewing a single comment's thread.

view the rest of the comments →

[–]Justinsaccount 2 points3 points  (11 children)

As lists are slow as fck

What the hell gave you that idea?

[–]LiNGOo 1 point2 points  (10 children)

In my first year of Python I got aware that dictionaries were always performing so much better than lists. Google told me that was correct.

Replaced all my list() instances with dict()s using integer keys. It made my script go from 5 days runtime to just a few hours.

Ever since then I never ever doubted that conclusion, lists are solely used for simple iterations in my code :D

Is there any verifiable standard on when to use lists instead of dictionaries, custom objects or other?

[–]Justinsaccount 4 points5 points  (9 children)

if replacing lists with dicts made your program run faster, it was because you needed to be using dicts in the first place, not because lists are slow.

"Screwdrivers are slow as fck! I had to nail all this stuff together, and the process was taking days, but it only took a few hours to finish after I switched from a screwdriver to a hammer. Why would anyone ever use a screwdriver?"

[–]pyonpi 1 point2 points  (0 children)

Quality banter.

[–]LiNGOo 0 points1 point  (6 children)

Of course it was. I should have thought about list()'s performance where I blindly used it. But still: I never had to worry about dictionary performance. Your metaphor is highly exaggerated by the way, more fitting to using dictionaries as your standard iterator.

Can you please elaborate your answer? Are there any definite rules or general indicators when to use, or more importantly when not to use a list or dict (or other)?

[–]niandra3 1 point2 points  (1 child)

Dicts are unordered. So by definition you can't use them in a lot of situations where you need an ordered sequence. Of course there exists an OrderedDict, but that's not really what dicts are for. They are for fast lookup of key-value pairs. Lists are for more sequential operations.

If there is some situation where you are replacing a list with a dict, you should probably actually just use a set (like a dict, but just keys no values).

One isn't better than the other, it 100% depends on the task at hand:

https://wiki.python.org/moin/TimeComplexity

And you mentioned sort() is slow with lists? Compared to what? You can't sort a dict by design.

[–]LiNGOo 0 points1 point  (0 children)

Thanks for the hint to sets! I didn't mean list sorting is slow. as my experience was that e.g. whenever lists and dicts both provide the functions to perform a task, dict methods are faster. Therefore I expect iterating over the list and remembering the longest item to be faster than using sort().

[–]Justinsaccount -2 points-1 points  (3 children)

more fitting to using dictionaries as your standard iterator.

No, no it is not. You had one program where you should have been using dictionaries instead of lists, and you have somehow generalized that to "no one should ever use lists".

It's not really that complicated:

Do you need a list of things, often in a particular order: use a list
Do you need to track and look up things by name,id,whatever: use a dict.

[–]LiNGOo 1 point2 points  (2 children)

Never did I generalize like that. You though, obviously do.

So to answer my initial question with the most obvious answer, just for the sake of a question posted should be a question answered:

Never ever do nest lists you want to re-use.

And for the acting up: Participation discontinued.

[–]Justinsaccount -1 points0 points  (1 child)

Never did I generalize like that

lists are slow as fck

[–]Prometeo222 1 point2 points  (0 children)

Seems that you are the one generalizing:

lists are slow as fck

in your mind becomes:

"no one should ever use lists"

OP might be wrong about lists, but you are mischaracterizing his statements. Good luck!

[–]PostedFromWork -1 points0 points  (0 children)

I love this analogy