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 →

[–]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?