Sometimes I worry the answers we the community give in response to questions like this are a bit hard to read. E.g. there’s something surgically efficient about foreach(((k, v), ) -> d[k] = v, zip(x2, y2)), but it’s pretty hard to parse: nested tuples in an anonymous function, etc.
If I were a new-ish user and asked how to do something that seems like it should be simple and got advice about fancy higher order functions and avoiding allocation, I would probably be put off. Even as an intermediate user, it feels somehow wrong that the right way to add multiple items to a dictionary requires broadcasting over a Ref or an anonymous function in a foreach or something like that.
Anyway, my not-high-powered answer to the original question is to not fear the loop: loops are typically fast in Julia, and it’s not lot of characters. You can even do it in one line if you want:
for (k,v) in zip(x2, y2) d[k] = v end
(though in my opinion it’s more readable with line breaks)
Shuhua, is there a particular reason you don’t want to loop?
Certainly no. I just want to know whether there are more convenient (built-in) functions like push! and append!.
Anyway, I have learned some advanced techniques in all these answers. Thanks.
For now, I think the for loop is the best solution: intuitive and fast. (Also, no need to put the loop in one line just to make it look shorter )