# Iterate over a collection and create a map

**URL:** <https://discuss.kotlinlang.org/t/iterate-over-a-collection-and-create-a-map/808>\
**Category:** Libraries\
**Created:** [May 24, 2013, 7:22pm UTC](https://discuss.kotlinlang.org/t/iterate-over-a-collection-and-create-a-map/808 "2013-05-24T19:22:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vlan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/vlan/32/1590_2.png) [@vlan](https://discuss.kotlinlang.org/u/vlan)\
**Post date:** [May 24, 2013, 7:22pm UTC](https://discuss.kotlinlang.org/t/iterate-over-a-collection-and-create-a-map/808/1 "2013-05-24T19:22:57Z")

</div>

I need something like Python dict comprehensions:

```kotlin
id_to_name = {p.id: p.name for p in persons}
```

which is equivalent to:

```kotlin
id_to_name = dict((p.id, p.name) for p in persons)
```

In Kotlin I have to do it this way:

```kotlin
idToName = persons.map { it.id to it.name }.toMap()
```

where toMap is:

```kotlin
fun <K, V> Iterable<Pair<K, V>>.toMap(): Map<K, V> {
  val result = HashMap<K, V>()
  for (x in this) {
    val (k, v) = x
    result.put(k, v)
  }
  return result
}
```

What's the best way of creating a map in Kotlin for this task? Is there something like toMap in stdlib?

---

<div class="post-metadata">

**Author:** ![apomelov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/apomelov/32/1546_2.png) [@apomelov](https://discuss.kotlinlang.org/u/apomelov)\
**Post date:** [May 24, 2013, 8:41pm UTC](https://discuss.kotlinlang.org/t/iterate-over-a-collection-and-create-a-map/808/2 "2013-05-24T20:41:57Z")

</div>

The only "oneliner" I've found is:

```kotlin
persons.fold(HashMap<Int, String>(), { map, person -> map.put(person.id, person.name); map })
```

Not so pretty as python's version...

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [May 25, 2013, 2:58pm UTC](https://discuss.kotlinlang.org/t/iterate-over-a-collection-and-create-a-map/808/3 "2013-05-25T14:58:37Z")

</div>

Looks like it's a good thing to add to our standard library. A pull request would be welcome :)

---

<div class="post-metadata">

**Author:** ![Russel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/russel/32/1442_2.png) [@Russel](https://discuss.kotlinlang.org/u/Russel)\
**Post date:** [June 18, 2016, 6:55pm UTC](https://discuss.kotlinlang.org/t/iterate-over-a-collection-and-create-a-map/808/4 "2016-06-18T18:55:24Z")

</div>

Did anything happen on this? i.e. list, set and “dictionary” comprehensions.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [June 19, 2016, 1:53am UTC](https://discuss.kotlinlang.org/t/iterate-over-a-collection-and-create-a-map/808/5 "2016-06-19T01:53:36Z")

</div>

No, there is no comprehensions available, but [toMap](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/to-map.html) and [associateBy](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/associate-by.html) extension functions are at your disposal.

---

<div class="post-metadata">

**Author:** ![Russel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/russel/32/1442_2.png) [@Russel](https://discuss.kotlinlang.org/u/Russel)\
**Post date:** [June 23, 2016, 9:22am UTC](https://discuss.kotlinlang.org/t/iterate-over-a-collection-and-create-a-map/808/6 "2016-06-23T09:22:22Z")

</div>

Whilst I knew of groupBy I wasn’t aware of associateBy, I shall investigate.
