# Itertating a 2D matrix

**URL:** <https://forum.elixirforum.com/t/itertating-a-2d-matrix/50849>\
**Category:** Questions\
**Tags:** matrix, learning-elixir, list, help, 2d-arrays\
**Created:** [October 7, 2022, 8:56pm UTC](https://forum.elixirforum.com/t/itertating-a-2d-matrix/50849 "2022-10-07T20:56:38Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anshul-13J](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/anshul-13j/32/28023_2.png) [@Anshul-13J](https://forum.elixirforum.com/u/Anshul-13J)\
**Post date:** [October 7, 2022, 8:56pm UTC](https://forum.elixirforum.com/t/itertating-a-2d-matrix/50849/1 "2022-10-07T20:56:38Z")

</div>

Hey everyone, I am new to Elixir language and I am having some issues while writing a piece of code.

What I am given is a 2D array like

```elixir
list1 = [
           [1 ,2,3,4,"nil"],
           [6,7,8,9,10,],
           [11,"nil",13,"nil",15],
           [16,17,"nil",19,20] ]

```

Now, what I’ve to do is to get all the elements that have values between 10 and 20, so what I’m doing is:

```elixir

final_list = []
Enum.each(list1, fn row ->   
Enum.each(row, &(if (&1 >= 10 and &1 <= 99) do final_list = final_list ++ &1 end)) 
end
)

```

Doing this, I’m expecting that I’ll get my list of numbers in final\_list but I’m getting blank final list with a warning like:

```elixir
warning: variable "final_list" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)
  iex:5

:ok

```

and upon printing final\_list, it is not updated.

When I try to check whether my code is working properly or not, using IO.puts as:

```elixir
iex(5)> Enum.each(list1, fn row -> ...(5)> Enum.each(row, &(if (&1 >= 10 and &1 <= 99) do IO.puts(final_list ++ &1) end))     
...(5)> end
...(5)> )

```

The Output is:

10

```elixir

11
13
15
16
17
19
20
:ok

```

What could I possibly be doing wrong here? Shouldn’t it add the elements to the `final_list`?  
If this is wrong ( probably it is), what should be the possible solution to this?

Any kind of help will be appreciated.

---

<div class="post-metadata">

**Author:** ![arcyfelix](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/arcyfelix/32/13683_2.png) [@arcyfelix](https://forum.elixirforum.com/u/arcyfelix)\
**Post date:** [October 7, 2022, 9:09pm UTC](https://forum.elixirforum.com/t/itertating-a-2d-matrix/50849/2 "2022-10-07T21:09:40Z")

</div>

In your specific example, it is better to use [Enum.reduce](https://hexdocs.pm/elixir/1.12/Enum.html#reduce/2).

---

<div class="post-metadata">

**Author:** ![Anshul-13J](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/anshul-13j/32/28023_2.png) [@Anshul-13J](https://forum.elixirforum.com/u/Anshul-13J)\
**Post date:** [October 7, 2022, 9:16pm UTC](https://forum.elixirforum.com/t/itertating-a-2d-matrix/50849/3 "2022-10-07T21:16:08Z")

</div>

On it! giving it a try

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [October 7, 2022, 9:17pm UTC](https://forum.elixirforum.com/t/itertating-a-2d-matrix/50849/4 "2022-10-07T21:17:24Z")

</div>

Hello and welcome,

The code You try to write is not going to work in FP, for many reasons… mostly because data is immutable, and scope is strict. What is defined in a do end block will disappear when the block end.

> [@Anshul-13J](#):
>
> a 2D array like

It’s not an array, it’s a list of list, more prcisely they are linked lists, and do not have the same properties (Not done for index access)

> [@Anshul-13J](#):
>
> ```elixir
> final_list = []
> Enum.each(list1, fn row ->   
> Enum.each(row, &(if (&1 >= 10 and &1 <= 99) do final_list = final_list ++ &1 end)) 
> end
> )
> 
> ```

The final list is not going to leak into the wrapping code.  
Using Enum.each is procedural

What You should do

```elixir
final_list = Enum.map(row, ...)

```

but because You don’t want nil, You could…

```elixir
final_list = row
|> Enum.map(...)
|> Enum.filter(...)

```

or better, as mentionned…

```elixir
final_list = row
|> Enum.reduce([], fn el, acc -> 
  if ..., do: [el | acc], else: acc
end)

```

You need to use other tools, like the Enum module

---

<div class="post-metadata">

**Author:** ![Anshul-13J](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/anshul-13j/32/28023_2.png) [@Anshul-13J](https://forum.elixirforum.com/u/Anshul-13J)\
**Post date:** [October 7, 2022, 9:22pm UTC](https://forum.elixirforum.com/t/itertating-a-2d-matrix/50849/5 "2022-10-07T21:22:38Z")

</div>

Gave it a try but still not working!

```elixir
iex(12)> Enum.each(list1, fn row -> ...(12)> Enum.reduce(row, fn x, final_list -> final_list = [final_list | x] end) 
...(12)> end
...(12)> )
warning: variable "final_list" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)
  iex:13

:ok
iex(13)> final_list
[]

```

---

<div class="post-metadata">

**Author:** ![Anshul-13J](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/anshul-13j/32/28023_2.png) [@Anshul-13J](https://forum.elixirforum.com/u/Anshul-13J)\
**Post date:** [October 7, 2022, 9:24pm UTC](https://forum.elixirforum.com/t/itertating-a-2d-matrix/50849/6 "2022-10-07T21:24:44Z")

</div>

Thanks for your valuable reply, looks like I gotta lot to learn!

Trying to implement it again 🥲

---

<div class="post-metadata">

**Author:** ![Anshul-13J](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/anshul-13j/32/28023_2.png) [@Anshul-13J](https://forum.elixirforum.com/u/Anshul-13J)\
**Post date:** [October 7, 2022, 9:33pm UTC](https://forum.elixirforum.com/t/itertating-a-2d-matrix/50849/7 "2022-10-07T21:33:24Z")

</div>

Tried this too:

```elixir
iex(14)> Enum.each(list1, fn row ->                                
...(14)> final_list = row                                          
...(14)> |> Enum.reduce([], fn el,acc ->                           
...(14)> if el >= 10 and el <= 99 do [el|acc] else acc end         
...(14)> end
...(14)> )
...(14)> end
...(14)> )

```

Still getting the same error 😢

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [October 7, 2022, 9:33pm UTC](https://forum.elixirforum.com/t/itertating-a-2d-matrix/50849/8 "2022-10-07T21:33:55Z")

</div>

You might often realize that You could solve something like this with only one command.

This would be my solution.

```elixir
final_list = Enum.reduce(row, [], fn 
  el, acc when el >= 10 and el <= 99 -> [el | acc] 
  _, acc -> acc
end)

```

You can see that You don’t need to initially set something to , You can set it in the function call.  
It uses an anonymous function with multiple head, and guard clause, no need to use if.  
You append an element to the list with [el | acc]  
You can minimiize local variables with the use of pipe.

Oh, my code is only for 1 D, for 2 D I need to nest…

There is another tool, You might use in FP, it’s a zipper.

> **[Zipper (data structure)](https://en.wikipedia.org/wiki/Zipper_(data_structure))**
>
> A zipper is a technique of representing an aggregate data structure so that it is convenient for writing programs that traverse the structure arbitrarily and update its contents, especially in purely functional programming languages. The zipper was described by Gérard Huet in 1997. It includes and generalizes the gap buffer technique sometimes used with arrays.
> The zipper technique is general in the sense that it can be adapted to lists, trees, and other recursively defined data structures.
> Suc...

---

<div class="post-metadata">

**Author:** ![dpreston](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@dpreston](https://forum.elixirforum.com/u/dpreston)\
**Post date:** [October 8, 2022, 12:07am UTC](https://forum.elixirforum.com/t/itertating-a-2d-matrix/50849/9 "2022-10-08T00:07:01Z")

</div>

From your initial question I think you are missing a key difference between Python and Elixir. Elixir variables are immutable and you cannot modify them like you would in Python.

```python
results = [] # <= create empty array
for x in range(1, 100):
  if 10 <= x <= 20:
    results.append(x) # <= append to the `results` array which is outside the `for` loop
  else:
   # skip x

# results = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

```

incorrectly translated to Elixir (similar to your initial attempt)

```elixir
results = [] # <= create an empty array
Enum.each(1..100, fn x ->
  if x >= 10 and x <= 20, do: results = results ++ [x] # <= does NOT append to the `results` list
end)

# warning: variable "results" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)

```

instead should be written

```elixir
results = Enum.reduce(1..100, [], fn x, acc -> # <= assign the final value of the accumulator to results
  if x >= 10 and x <= 20 do
    acc ++ [x] # <= append x to the accumulator
  else
    acc # <= do not modify the accumulator
  end
end)

# results = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

```

The difference is that `Enum.reduce` builds up the accumulator array and then assigns it all to `results` at the end, it doesn’t modify `results` as it processes each element of the list.  
In fact, it _cannot_ modify `results` even if we wanted to.

The full solution then looks like this,

```elixir
list1 = [
  [1, 2, 3, 4, "nil"],
  [6, 7, 8, 9, 10],
  [11, "nil", 13, "nil", 15],
  [16, 17, "nil", 19, 20]
]
final_list = Enum.reduce(list1, [], fn inner_list, acc ->
  acc ++ Enum.reduce(inner_list, [], fn x, acc2 ->
    if x >= 10 and x <= 20 do
      acc2 ++ [x]
    else
      acc2
    end
  end)
end)

# => [10, 11, 13, 15, 16, 17, 19, 20]

```

If you don’t need to retain the nested structure or process the elements further, I would suggest a different approach. First flatten the inner lists and then filter with a simple boolean test.

```elixir
final_list = list1 
|> List.flatten()
|> Enum.filter(&(&1 >=10 and &1 <= 20))

# final_list = [10, 11, 13, 15, 16, 17, 19, 20]

```

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [October 8, 2022, 1:08am UTC](https://forum.elixirforum.com/t/itertating-a-2d-matrix/50849/10 "2022-10-08T01:08:12Z")

</div>

Banging your head against the wall is a slow way to learn, dude. Trust me I know, I’ve done the same way too many times.

You have to assign the result of almost every function to a variable in Elixir because it does NOT change a variable in place like in Python in many others; _it returns a modified copy of the variable_.

```elixir
list = []
Enum.each([1,2,3], fn x ->
  list = list ++ (x*2)
end)

```

List is still `[]` in the end because the internal expression `list = list ++ (x*2)` is staying there and not going anywhere.

If you run the above you’ll get this message:

```plaintext
warning: variable "list" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)

```

It’s telling you that you are throwing away `list` from inside the `Enum.each` block.

The way you would achieve the desired result is this:

```elixir
list = Enum.map([1,2,3], fn x ->
  x * 2
end)

```

This goes through each element of the list, modifies it, and appends it to a resulting list.

TL;DR forget about using `Enum.each` or `for` to modify variables. They are not used for that, only for side effects (like printing to terminal, writing to files or external network services etc.)

---

<div class="post-metadata">

**Author:** ![Anshul-13J](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/anshul-13j/32/28023_2.png) [@Anshul-13J](https://forum.elixirforum.com/u/Anshul-13J)\
**Post date:** [October 8, 2022, 6:02am UTC](https://forum.elixirforum.com/t/itertating-a-2d-matrix/50849/11 "2022-10-08T06:02:43Z")

</div>

Really helpful one!

The thing is, I am understanding the code, but having issues in writing it! The syntax is a bit changed from languages like Cpp and Python, but I guess I’ll get there. Just need some practice!

---

<div class="post-metadata">

**Author:** ![gregvaughn](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gregvaughn/32/954_2.png) [@gregvaughn](https://forum.elixirforum.com/u/gregvaughn)\
**Post date:** [October 8, 2022, 6:50pm UTC](https://forum.elixirforum.com/t/itertating-a-2d-matrix/50849/12 "2022-10-08T18:50:41Z")

</div>

The real issue to understand here is immutability. `Enum.each` _always_ returns `:ok` and it has no access to variables outside of its scope, so there is no use in assigning to a var within a function passed to `Enum.each`. As you gain more experience in functional style programming, you’ll start thinking in higher-order functions. Those are great building blocks that can be combined in many ways to make your exercise clear and concise (to those familiar with functional style). Here’s two ways I’d solve it:

```elixir
iex(63)> list1 = [
...(63)> [1 ,2,3,4,"nil"],
...(63)> [6,7,8,9,10,],
...(63)> [11,"nil",13,"nil",15],
...(63)> [16,17,"nil",19,20] ]
[
  [1, 2, 3, 4, "nil"],
  [6, 7, 8, 9, 10],
  [11, "nil", 13, "nil", 15],
  [16, 17, "nil", 19, 20]
]
iex(64)> Enum.flat_map(list1, fn row -> Enum.filter(row, &(&1 in 10..20)) end)
[10, 11, 13, 15, 16, 17, 19, 20]
iex(65)> for row <- list1, elt <- row, elt in 10..20, do: elt
[10, 11, 13, 15, 16, 17, 19, 20]

```

In the first example, I’ve combined two higher-order functions (flat\_map and filter) with an anonymous function (invoking the `in` operator). The second is a for comprehension, which is more advanced. It should not be confused with a for loop. Comprehensions are much more powerful.

---

<div class="post-metadata">

**Author:** ![arcyfelix](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/arcyfelix/32/13683_2.png) [@arcyfelix](https://forum.elixirforum.com/u/arcyfelix)\
**Post date:** [October 9, 2022, 9:31pm UTC](https://forum.elixirforum.com/t/itertating-a-2d-matrix/50849/13 "2022-10-09T21:31:10Z")

</div>

Even in Python, this should have been using reduce().
