# Return List with Single Element

**URL:** <https://discourse.elm-lang.org/t/return-list-with-single-element/7835>\
**Category:** Learn\
**Created:** [October 19, 2021, 8:14am UTC](https://discourse.elm-lang.org/t/return-list-with-single-element/7835 "2021-10-19T08:14:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/simon/32/4310_2.png) [@Simon](https://discourse.elm-lang.org/u/Simon)\
**Post date:** [October 19, 2021, 8:14am UTC](https://discourse.elm-lang.org/t/return-list-with-single-element/7835/1 "2021-10-19T08:14:08Z")

</div>

Hi,

I am new to Elm and I have a function that returns a list with elements.  
I want to edit this function to return a single element list with a text if the list is empty.  
E.g “Could not retrieve list for you”

Original code

```auto
viewResults : RemoteData.WebData (List KartverketResult) -> Html Msg
viewResults data =
    case data of
        RemoteData.NotAsked ->
            text ""

        RemoteData.Loading ->
            text ""

        RemoteData.Success results ->
            results
                |> List.map viewResult
                |> ul [class "col-1of1 border_search_list"]

        RemoteData.Failure e ->
            text "Sorry, could not retrieve list. Some error occurde"

```

Ish what I wish, that Elm does not want to do for me:

```auto
viewResults : RemoteData.WebData (List KartverketResult) -> Html Msg
viewResults data =
    case data of
        RemoteData.NotAsked ->
            text ""

        RemoteData.Loading ->
            text ""

        RemoteData.Success results ->
            if List.isEmpty results then
                li [class "col-1of1 txt-primary padding-5"] [text "List is empty, Please check your spelling"] 
                |> ul [class "col-1of1 border_search_list"]

            else
                results
                    |> List.map viewResult
                    |> ul [class "col-1of1 border_search_list"]

        RemoteData.Failure e ->
            text "Sorry, could not retrieve list. Some error occurde"

```

tl;dr

```auto
RemoteData.Success results ->
            results
                |> List.map viewResult
                |> ul []

```

Code above to code below

```auto
RemoteData.Success results ->
            if List.isEmpty results then
                li [] [text "List is empty, Please check your spelling"] 
                |> ul []

            else
                results
                    |> List.map viewResult
                    |> ul []

```

Kind Regards,  
Simon

---

<div class="post-metadata">

**Author:** ![edkelly303](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/edkelly303/32/1510_2.png) [@edkelly303](https://discourse.elm-lang.org/u/edkelly303)\
**Post date:** [October 19, 2021, 9:19am UTC](https://discourse.elm-lang.org/t/return-list-with-single-element/7835/2 "2021-10-19T09:19:51Z")

</div>

```auto
if List.isEmpty results then
    li [] [text "List is empty, Please check your spelling"] 
        |> ul []

```

I think the problem here is that `ul []` needs a list of `li`s, and you’re only giving it a single `li`. So you need to wrap your `li` in a list.

Here are two ways to do that:

```auto
if List.isEmpty results then
    [li [] [text "List is empty, Please check your spelling"] ]
        |> ul []

```

or:

```auto
if List.isEmpty results then
    li [] [text "List is empty, Please check your spelling"] 
        |> List.singleton
        |> ul []

```

---

<div class="post-metadata">

**Author:** ![Simon](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/simon/32/4310_2.png) [@Simon](https://discourse.elm-lang.org/u/Simon)\
**Post date:** [October 19, 2021, 10:22am UTC](https://discourse.elm-lang.org/t/return-list-with-single-element/7835/3 "2021-10-19T10:22:35Z")

</div>

Hello Rasputin303,

Thank you so much!  
Both of these solutions worked exactly how I wanted.

Hope you have a nice day,  
Warm regards,  
Simon

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [October 29, 2021, 10:23am UTC](https://discourse.elm-lang.org/t/return-list-with-single-element/7835/4 "2021-10-29T10:23:34Z")

</div>

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