-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
show(dict) now sorted by key #16743
show(dict) now sorted by key #16743
Conversation
-1, I think the danger here is that a user sees the output of a Dict and assumes a certain order, then iterates over the key-value pairs expecting that same order. I don't think we should do this unless we truly move to an ordered dict representation; ref #10116 |
Furthermore, with ordered dicts the order is significant, but not necessarily the same as the sorted order. |
@bjarthur, just in case you aren't aware, DataStructures.jl contains an actual |
thanks for the reminder about does seem a bit cumbersome though as i never care about the unsorted order. my concern is just about quickly finding by eye the key/value i'm looking for. for example:
perhaps i should ask beforehand whether i should tackle REPL completions on Dict keys. i want to be able to |
+1. |
This has performance implications for printing very large Dicts. I don't think it's a good idea. I do, however, think that making Dict ordered is a good idea that we should go ahead with. |
@Keno are there any plans to implement tab completion on Dict keys? is this a feature that would be favorably viewed? i'm willing to take a stab at it if it's not already on the horizon. |
There are indeed performance implications, but without sorting, showing the dict amounts to showing randomly (more or less) selected element, which is not ideal either. Anyway, if Dict becomes ordered in the future, the status-quo is fine for now. |
@StefanKarpinski I think ordered dict by default is a really bad idea. ordered dicts have their place, but there are a large number of places where the order makes no difference for the algorithm, and making dict ordered by default would slow down these programs significantly (up to ~2x if dict read/writes are where most time is spent). I do think that there should be an ability show dicts in order, but I think that this should be a choice. |
I assume there are also cases where the keys do not even have a well-defined order... |
specifically strings, vectors etc. |
I'm talking about ordered dicts, not sorted dicts. An ordered dict maintains the ordering in which keys are inserted; in particular, it does not require the keys to be comparable. @JeffBezanson did an experiment where he changed Julia's Dict type to be ordered without sacrificing speed in most cases. There were a few cases where it was slower, but it was not 2x. |
(That implementation is now part of DataStructures.jl) |
sorting vastly improves human readability.
left intact is the unsorted compact output. someone might need to know the true order of the pairs in the dict. it is also not trivial to sort this due to the need to handle circularly referential dicts.