I want to get any key/value pair from associative array and remove it. In python it's:
key, value = assoc.popitem()
In D I do:
auto key = assoc.byKey.front;
auto value = assoc[key];
assoc.remove(key);
Is there better way to do this? Is it possible to use byKeyValue() outside foreach?
DMD 2.067.1
Is it possible to use byKeyValue() outside foreach?
Sure:
import std.stdio;
void main()
{
int[string] assoc = ["apples" : 2, "bananas" : 4];
while (!assoc.byKeyValue.empty)
{
auto pair = assoc.byKeyValue.front;
assoc.remove(pair.key);
writeln(pair.key, ": ", pair.value);
}
}
Is there better way to do this?
I don't think D has a library function equivalent for popitem
.