> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/sidef-lang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/sidef-lang/programming_tasks/f/flatten_a_list.md).

# Flatten a list

```ruby
func flatten(a) {
    var flat = []
    a.each { |item|
        flat += (item.kind_of(Array) ? flatten(item) : [item])
    }
    return flat
}

var arr = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
say flatten(arr)      # used-defined function
say arr.flatten       # built-in Array method
```
