# Why only vectors \`into\` map?

**URL:** https://clojureverse.org/t/why-only-vectors-into-map/4966
**Category:** General Questions
**Tags:** clojure
**Created:** [October 16, 2019, 8:47pm UTC](https://clojureverse.org/t/why-only-vectors-into-map/4966 "2019-10-16T20:47:23Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Webdev\_Tory](https://clojureverse.org/user_avatar/clojureverse.org/webdev_tory/32/3781_2.png) [@Webdev\_Tory](https://clojureverse.org/u/Webdev_Tory)
#### Post date: [October 16, 2019, 8:47pm UTC](https://clojureverse.org/t/why-only-vectors-into-map/4966/1 "2019-10-16T20:47:23Z")

</div>

Why does `into {}` fail when given a list, but succeed with a vector?

```clojure
(let [cols [:id :label]
      data '({:id 1 :label "one"} {:id 2 :label "two"})]
;;(into {} (map #(map % cols) data)) ;; Execution error (ClassCastException) at (REPL:1).
  (into {} (map #(mapv % cols) data))
  ;; {1 "one", 2 "two"}
  ) 

```

---

<div class="post-metadata">

### Author: ![borkdude](https://clojureverse.org/user_avatar/clojureverse.org/borkdude/32/306_2.png) [@borkdude](https://clojureverse.org/u/borkdude)
#### Post date: [October 16, 2019, 9:22pm UTC](https://clojureverse.org/t/why-only-vectors-into-map/4966/2 "2019-10-16T21:22:13Z")

</div>

The behavior of `conj` with maps is:

1. when provided a `MapEntry` it adds it to the map (as in associates key and value)
2. when provided a vector, same as 1) but it treats the 0th position of the vector as the key and the 1st position as the value to be associated with the map
3. when provided other seqables than a vector, it treats it as a seqable of MapEntries, so it’s a repeated version of 1). If the seqable does not contain MapEntries then you’ll run into a ClassCastException.

The rationale for 2 could be that creating vectors is much more ergonomic than having to create `MapEntry` instances.

The rationale for 3 could be that these kinds of operations work:

```auto
(conj {:a 1} (filter (fn [[k v]] (odd? v)) {:b 2 :c 3}))
;;=> {:a 1, :c 3}

```

Now I’ll wait for Alex to give the official answer :P.

---

<div class="post-metadata">

### Author: ![alexmiller](https://clojureverse.org/user_avatar/clojureverse.org/alexmiller/32/4805_2.png) [@alexmiller](https://clojureverse.org/u/alexmiller)
#### Post date: [October 17, 2019, 2:03am UTC](https://clojureverse.org/t/why-only-vectors-into-map/4966/3 "2019-10-17T02:03:02Z")

</div>

LGTM borkdude 🙂

I guess the only thing to add is that a list of 2 elements is not treated as a valid map entry because it is not indexed (you can’t just grab the 0th key “slot” and the 1th value “slot”), you would have to iterate through the key to get to the value.

---

<div class="post-metadata">

### Author: ![Webdev\_Tory](https://clojureverse.org/user_avatar/clojureverse.org/webdev_tory/32/3781_2.png) [@Webdev\_Tory](https://clojureverse.org/u/Webdev_Tory)
#### Post date: [October 17, 2019, 4:16pm UTC](https://clojureverse.org/t/why-only-vectors-into-map/4966/4 "2019-10-17T16:16:56Z")

</div>

Ah; so lists fail ducktyping as vectors so are handled differently on map `conj`. I can remember that. Thanks!

---

<div class="post-metadata">

### Author: ![seancorfield](https://clojureverse.org/user_avatar/clojureverse.org/seancorfield/32/195_2.png) [@seancorfield](https://clojureverse.org/u/seancorfield)
#### Post date: [January 22, 2020, 6:13pm UTC](https://clojureverse.org/t/why-only-vectors-into-map/4966/5 "2020-01-22T18:13:03Z")

</div>

> [@borkdude](#):
>
> - when provided a vector, same as 1) but it treats the 0th position of the vector as the key and the 1st position as the value to be associated with the map

Probably worth the clarification that it has to be a 2-element vector. If the vector has 3 or more elements, you’ll get `Vector arg to map conj must be a pair` (`IllegalArgumentException`).

---

<div class="post-metadata">

### Author: ![system](https://clojureverse.org/uploads/default/original/2X/5/51079bf9e4b7d9466242c06cf1e43b9f8bd6da14.png) [@system](https://clojureverse.org/u/system)
#### Post date: [July 23, 2020, 6:13am UTC](https://clojureverse.org/t/why-only-vectors-into-map/4966/6 "2020-07-23T06:13:06Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
