# Mapping over nested lists

**URL:** <https://discourse.elm-lang.org/t/mapping-over-nested-lists/756>\
**Category:** Learn\
**Created:** [February 24, 2018, 4:40pm UTC](https://discourse.elm-lang.org/t/mapping-over-nested-lists/756 "2018-02-24T16:40:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)\
**Post date:** [February 24, 2018, 4:40pm UTC](https://discourse.elm-lang.org/t/mapping-over-nested-lists/756/1 "2018-02-24T16:40:22Z")

</div>

I have a model that is a record in which some fields are lists of lists of strings. I’d like to display each item of the nested list in the view. This seems like a simple thing that I’m just not getting right.

```
myModel = { field1 = ["foo", "bar", "baz"], field2 = [["this", "that"], ["the", "other"] ] }

```

I’m trying to do something like:

```
List.map Html.text (List.map (\_ x -> x) myModel.field2)

```

but that doesn’t work due to the second argument to map being a `List (String -> String)` instead of a `List (String)`.

---

<div class="post-metadata">

**Author:** ![michaeljones](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/michaeljones/32/152_2.png) [@michaeljones](https://discourse.elm-lang.org/u/michaeljones)\
**Post date:** [February 24, 2018, 5:57pm UTC](https://discourse.elm-lang.org/t/mapping-over-nested-lists/756/2 "2018-02-24T17:57:48Z")

</div>

I’m not sure what your intention with the `(\_ x -> x)` function is? Are you wanting to print the second entry in each list?

Otherwise, it sounds like you might benefit from concatenating the lists first. So you could do:

```auto
myModel.field2 |> List.concat |> List.map Html.text

```

Which is the same as:

```auto
List.map Html.text (List.concat myModel.field2)

```

You might find that sufficient. If you want to do the second item in each list, it would be more involved.

---

<div class="post-metadata">

**Author:** ![pdamoc](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/pdamoc/32/36_2.png) [@pdamoc](https://discourse.elm-lang.org/u/pdamoc)\
**Post date:** [February 25, 2018, 8:02am UTC](https://discourse.elm-lang.org/t/mapping-over-nested-lists/756/3 "2018-02-25T08:02:18Z")

</div>

> [@stevensonmt](#):
>
> `List.map Html.text (List.map (\_ x -> x) myModel.field2)`

this is pointing to a slight misunderstanding. If you have `[["foo", "bar"]]` you cannot treat the internal list as a list of two arguments because it is not modeled like that. I order to model it like that you need to use tuples.

`List.map Html.text (List.map (\(_, x) -> x) [("foo", "bar")])` would work and be equivalent to `List.map Html.text (List.map Tuple.first [("foo", "bar")])`

if you still want to model it like that, you will need something like:

```elm
    List.map
        (\l ->
            case l of
                _ :: x :: [] ->
                    text x

                _ ->
                    text ""
        )
        myModel.field2

```

or ` List.map (\l -> List.drop 1 |> List.head |> Maybe.withDefault "" |> text) myModel.filed2`

---

<div class="post-metadata">

**Author:** ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)\
**Post date:** [March 13, 2018, 3:41am UTC](https://discourse.elm-lang.org/t/mapping-over-nested-lists/756/4 "2018-03-13T03:41:11Z")

</div>

Thanks for the help guys. I ended up restructuring the model to make life simpler but your comments improved my overall understanding. Thanks again.
