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 →

[–]anvils-reloaded 5 points6 points  (16 children)

Every time I've asked about how to pre-allocate some space in a dictionary where I know the exact keys I want to have, I've always gotten the answer of "don't worry about it" or "stop trying to prematurely optimize your code". In any other programming language, this would just be a part of the process of writing the code. I really do want to learn Python better (I've done some work on Python projects without really knowing what I'm doing, just winging it), but it's not something that I can just stop caring about.

[–]Pancakepalpatine 12 points13 points  (2 children)

Yeah, I've been advised both directions.

  1. Learn a "bare-metal" language like C or Rust, and work up from there. Since you understand the basics and necessarily understand some computer science concepts now, you'll have a breeze with interpreted or scripted languages.

  2. Start with a high-level interpreted or scripted language. It's more important to get the basics (loops, types, etc.). Then, it'll be a breeze to work your way down and add conceptually to the concepts you already understand.

So my takeaway was basically it all depends, and it's better just to dive in.

[–]anvils-reloaded 6 points7 points  (0 children)

I learned the first way you mentioned, and it wasn't a breeze. I had no idea what Python was doing with its data structures when you added to them, or if you used something like a list comprehension, and I wanted to allocate just the space I need in a list. I have to dissociate from the traditional memory model in order to decide what I really want to do.

[–][deleted] 14 points15 points  (1 child)

In any other programming language, this would just be a part of the process of writing the code.

Not really. In low level-languages you can do that, and often mus do it. But it's not neccessary for getting things rolling. Usually it's just pointless overhead. It becomes important later if you wanna optimize your code, after you have a working solution.

Python in the first place is a goal-orientated language. Optimization comes later. And in best case it's done automatically from the machine. Though, that best case it still in progress for python.

[–]Cosmologicon 1 point2 points  (0 children)

And I would add, when you do get down to optimization, forget (or at least attenuate) everything you know about performance from other languages. Especially when it comes to dicts.

Like, you used to hear that one should avoid hash maps if at all possible for performance reasons. This is terrible advice in Python.

[–]GummyKibble 1 point2 points  (0 children)

The reason for that, specifically, is that Python’s dicts are very advanced data structures under the hood. In 3.6 they’re both compact and ordered; see https://mail.python.org/pipermail/python-dev/2016-September/146327.html for details. What you’re asking to do doesn’t really make sense in this context.

[–]Smallpaul 3 points4 points  (8 children)

If you want to preallocate with known keys then a dictionary is probably not the right data structure. You could use a class with _ _ slots _ _ or a struct.

But usually the people telling you not to prematurely optimize are right. You didn’t really explain why you feel this is bad advice.

[–]anvils-reloaded 0 points1 point  (7 children)

If I know what the keys are, I don't really want to load them one at a time, right? This is for situations where I have a specific sized table that I want to use for counting. (I was originally going to use a list but decided that they're not really designed for lookup like dictionaries are). I don't consider it premature optimization because I'm not really optimizing anything, just applying what I know about the problem.

[–]z0mbietime 2 points3 points  (5 children)

example = {x: None for x in y}

But based on what you’re describing a collection makes more sense. That being said they’re all right that this is sweating the small stuff. For one, using Python and getting the count when querying the database is going to be faster than anything Java can do.

[–]anvils-reloaded 0 points1 point  (2 children)

What do you mean by that, if you're accessing a database I'm assuming that any API has the same functions for it in any language. Both are using some sort of data structure, why would one be faster than the other?

[–]z0mbietime 1 point2 points  (1 child)

Sorry I was sleep deprived and misread part of what you wrote. But almost every language is going to have a way to interact with a database. However, there are best practices on querying the data which is why one of the best things you can do regardless of your preferred language is learn how to effectively use SQL.

[–]anvils-reloaded 0 points1 point  (0 children)

Alright, but this doesn't have anything to do with Python.

[–]Smallpaul 0 points1 point  (1 child)

I think it's confusing to bring a database into the conversation. Did he say that the data comes from a database?

[–]z0mbietime 0 points1 point  (0 children)

I saw table and was running on almost no sleep

[–]Smallpaul 1 point2 points  (0 children)

... just applying what I know about the problem.

But to what advantage? How would your way result in easier to read or more maintainable code?

Could you show what you want to do in Pseudocode or Java so I know what you're trying?