holder66

holder66

Transpose a list of lists using list comprehension

Coming from Python and its version of list comprehensions, I am looking for a way to
accomplish in Elixir, transposition of a list of lists. Here is single line Python version:
>>> m = [[1,2],[3,4],[5,6]]
>>> rez = [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]
>>> rez
[[1, 3, 5], [2, 4, 6]]
I would like to keep it short and elegant, if possible! Thanks for your help!

Most Liked

kip

kip

ex_cldr Core Team

Or simply:

iex> Enum.zip [[1,2],[3,4],[5,6]]
[{1, 3, 5}, {2, 4, 6}]

# or if you need list of lists:
iex> Enum.zip([[1,2],[3,4],[5,6]]) |> Enum.map(&Tuple.to_list/1)
[[1, 3, 5], [2, 4, 6]]
10
Post #3
holder66

holder66

Wow, great answers! Thank you all!

idi527

idi527

:wave:

Adapting https://stackoverflow.com/questions/5389254/transposing-a-2-dimensional-matrix-in-erlang to elixir:

defmodule Transp do
  def transpose([[] | _]), do: []
  def transpose(m) do
    [Enum.map(m, &hd/1) | transpose(Enum.map(m, &tl/1))]
  end
end
iex(2)> matrix = [[1,2],[3,4],[5,6]]
[[1, 2], [3, 4], [5, 6]]
iex(3)> Transp.transpose(matrix)
[[1, 3, 5], [2, 4, 6]]
idi527

idi527

Another way (from http://erlang.org/pipermail/erlang-questions/2009-June/044609.html):

defmodule Transp do
  def transpose([]), do: []
  def transpose([[] | xss]), do: transpose(xss)
  def transpose([[x | xs] | xss]) do
    [[x | (for [h | _t] <- xss, do: h)] | transpose([xs | (for [_h | t] <- xss, do: t)])]
  end
end

although it’s practically the same.

Where Next?

Popular in Questions Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement