# List to Map with key unchanged

**URL:** <https://discuss.kotlinlang.org/t/list-to-map-with-key-unchanged/7612>\
**Category:** Libraries\
**Created:** [April 29, 2018, 8:39am UTC](https://discuss.kotlinlang.org/t/list-to-map-with-key-unchanged/7612 "2018-04-29T08:39:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discuss.kotlinlang.org/u/user1)\
**Post date:** [April 29, 2018, 8:39am UTC](https://discuss.kotlinlang.org/t/list-to-map-with-key-unchanged/7612/1 "2018-04-29T08:39:19Z")

</div>

I have list e.g. `1, 2, 3`. I want to map each value to `* 2`, i.e. `(1 to 2, 2 to 4, 3 to 6)`. Is there any stdlib function which can do this without explicitly restating the key?

I’d be looking to write something like `list.whatGoesHere {it * 2}`.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [April 29, 2018, 9:01am UTC](https://discuss.kotlinlang.org/t/list-to-map-with-key-unchanged/7612/2 "2018-04-29T09:01:56Z")

</div>

I don’t think there is. The best I could come up with is

```kotlin
list.map { it to it * 2 }.toMap()

```

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [April 29, 2018, 10:09am UTC](https://discuss.kotlinlang.org/t/list-to-map-with-key-unchanged/7612/3 "2018-04-29T10:09:58Z")

</div>

There is [associate](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/associate.html) extension function which does the same.

---

<div class="post-metadata">

**Author:** ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discuss.kotlinlang.org/u/user1)\
**Post date:** [April 29, 2018, 10:14am UTC](https://discuss.kotlinlang.org/t/list-to-map-with-key-unchanged/7612/4 "2018-04-29T10:14:03Z")

</div>

I’m using `associate` for lack of anything better.

Should have mentioned that the output doesn’t actually have to be a `Map`, a list of `Pair`s is also good. So Wasabi’s example would work without `.toMap()`.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [April 29, 2018, 10:17am UTC](https://discuss.kotlinlang.org/t/list-to-map-with-key-unchanged/7612/5 "2018-04-29T10:17:11Z")

</div>

As far, as I can grasp kotlin’s ideology, you do not have to have anything in the standard library. If you need it, extend it. It does not produce java problem of multiple classes doing the same thing in different libraries, since extensions are not members.

---

<div class="post-metadata">

**Author:** ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discuss.kotlinlang.org/u/user1)\
**Post date:** [April 29, 2018, 10:20am UTC](https://discuss.kotlinlang.org/t/list-to-map-with-key-unchanged/7612/6 "2018-04-29T10:20:34Z")

</div>

I don’t think any language requires you to have something in the standard library instead of writing it yourself 🙂 (I assume you refer to extension functions.)

Just seems like an odd omission given all the other utility functions for transforming lists to maps, and Kotlin’s general tendency for lots of very similar utility functions in the standard library (e.g. let/run/apply/also/with for/map/foreach etc).
