# How do I get a list of indices of non zero elements in a list?

**URL:** <https://discuss.kotlinlang.org/t/how-do-i-get-a-list-of-indices-of-non-zero-elements-in-a-list/6828>\
**Category:** Language Design\
**Created:** [February 28, 2018, 4:47pm UTC](https://discuss.kotlinlang.org/t/how-do-i-get-a-list-of-indices-of-non-zero-elements-in-a-list/6828 "2018-02-28T16:47:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![niksw7](https://avatars.discourse-cdn.com/v4/letter/n/aca169/32.png) [@niksw7](https://discuss.kotlinlang.org/u/niksw7)\
**Post date:** [February 28, 2018, 4:47pm UTC](https://discuss.kotlinlang.org/t/how-do-i-get-a-list-of-indices-of-non-zero-elements-in-a-list/6828/1 "2018-02-28T16:47:18Z")

</div>

a=[0,1,0,1,0,0]  
[i for i, e in enumerate(a) if e != 0]  
The above code snippet in python will give me indices viz:1,3

I was trying to figure out how to do it in kotlin in same ease.  
Any pointers if it exists?

---

<div class="post-metadata">

**Author:** ![niksw7](https://avatars.discourse-cdn.com/v4/letter/n/aca169/32.png) [@niksw7](https://discuss.kotlinlang.org/u/niksw7)\
**Post date:** [February 28, 2018, 4:56pm UTC](https://discuss.kotlinlang.org/t/how-do-i-get-a-list-of-indices-of-non-zero-elements-in-a-list/6828/2 "2018-02-28T16:56:53Z")

</div>

val array = intArrayOf(0, 0, 1, 0, 1, 0, 0, 1)  
print(array.indices.filterIndexed { \_, i → array[i] != 0 })

This is what I have got. Looks good though, but if anyone has better way to do it, please reply

---

<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:** [February 28, 2018, 6:45pm UTC](https://discuss.kotlinlang.org/t/how-do-i-get-a-list-of-indices-of-non-zero-elements-in-a-list/6828/3 "2018-02-28T18:45:19Z")

</div>

> [@niksw7](#):
>
> print(array.indices.filterIndexed { \_, i -\> array[i] != 0 })

You can just use

```Kotlin
val array = intArrayOf(0, 0, 1, 0, 1, 0, 0, 1)
print(array.indices.filtered { array[it] != 0 })

```

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [March 6, 2018, 3:43pm UTC](https://discuss.kotlinlang.org/t/how-do-i-get-a-list-of-indices-of-non-zero-elements-in-a-list/6828/4 "2018-03-06T15:43:22Z")

</div>

> [@Wasabi375](#):
>
> val array = intArrayOf(0, 0, 1, 0, 1, 0, 0, 1)  
> print(array.indices.filtered { array[it] != 0 })

It’s filter, not filtered

When doing Kotlin I often feel dirty creating local variables so I try to find ways to avoid creating local variables and this is what I came up with here:

```
intArrayOf(0, 0, 1, 0, 1, 0, 0, 1)
        .mapIndexed{i, v -> if(v != 0) i else -1}
        .filter { it >= 0 }

```

It would not be difficult to do your own extension method for this operation as well.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [March 6, 2018, 4:03pm UTC](https://discuss.kotlinlang.org/t/how-do-i-get-a-list-of-indices-of-non-zero-elements-in-a-list/6828/5 "2018-03-06T16:03:03Z")

</div>

```kotlin
listOf(0, 0, 1, 0, 1, 0, 0, 1).mapIndexedNotNull { i, n -> i.takeIf { n != 0 } }

```
