Keyword Lists - why do I get this result?

[a: “1”, b: “2”]

results in

[a: “1”, b: “2”]

but

[1, a: “1”, b: “2”]

results in

[1, {:a, “1”}, {:b, “2”}]

Why?

Looks like two different syntaxes for the same thing: https://tio.run/##S83JrMgs@v/f018vM6@4IDW5RCHaUEeh2ipRR8GwFsRI0lEwqo1VsLUFSyRaKQDJJCsFo9j//wE

[a: "1"] is just sugar syntax to represent keyword lists, e.g. [{:a, "1"}].

If you have another element different than {atom, term} it will print weirdly, as in normal list, not keyword list anymore.

2 Likes

@rodrigues please correct typo [ instead of } because your answer will help others.

what typo? :stuck_out_tongue: (oops, just seen it :+1:t3::rocket:)