I hated using dictionaries in Python, mostly because they in-memory only. I guess I am so accustomed to relational DBMS’s that I just can’t think any other way. So, truly, are dictionaries that useful in Julia? Are they even used anywhere except for the most trivial of tasks? Not trying to rain on anyone’s parade, trying to figure out how much study I should to attach to (re)learning them again. (Not a big fan of Python.)
Well they are a tool at your disposal. If you don’t like them, then don’t use them
Personally, I have barely used them. I think this is because they are less convenient to use in Julia than in Python, so I tend reach for other solutions.
Dictionaries are not that great for large or performance-critical data, but outside of that they are enormously useful, and used, for a large variety of tasks.
I personally found dictionaries and Sets (which are just special dicts) to be immensely useful for some tasks which require bookkeping and frequent if i in array
kind of tasks.
Some of these problems are quite possibly much slower with standard arrays. In any case Dicts are very natural for them.
why do you say this? Dict
should be pretty performant across the general-purpose use case it is intended for. of course with more information about the specific problem at hand, often one can tailor a data structure for more performance (on that specific problem)
Of course they are fine for use cases they are intended for but they won’t help you when you want memory locality or SIMD optimizations.
But yes, if you ever need to repeatedly check if an element is in a large collection, place the collection in a Set
and not in an Array
(well, unless you can organize it as a lookup in a BitVector
).
There are tasks for which dictionaries are the most suitable, such as counting word frequencies.
Dictionaries.jl can do a lot of operations with memory locality
Dictionaries are a basic abstract data type, and in-memory hash table implementations for O(1) indexing are fairly widespread and similar across many languages, including application e.g. memoization, dynamic fields. A couple practical differences between Python and Julia is that Julia has a wider variety of implementations used in practice, and they have type parameters for the keys and values.
A dictionary is too different from a relational database to be worth comparing, conceptually or in usual implementations. A dataframe/table would get closer (and could use a dictionary as a component), but still not close. I think you’re right that Python, or mostly teaching materials using it, exaggerate how useful dictionaries are alone because tutorials tend to overuse base features to avoid the learning curve of properly installing useful libraries. Implementing a database with dict
and list
outside of a toy example is as miserable as doing number crunching with list
comprehensions instead of numpy.ndarray
.
I think nothing prevents you from implementing your own struct that derives AbstractDict
, you can even implement your file based one or even DB based one.