# How to avoid the nested list in the command table?

**URL:** <https://community.wolfram.com/t/how-to-avoid-the-nested-list-in-the-command-table/5056>\
**Category:** Questions\
**Tags:** mathematica\
**Created:** [March 8, 2015, 11:02pm UTC](https://community.wolfram.com/t/how-to-avoid-the-nested-list-in-the-command-table/5056 "2015-03-08T23:02:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fraccalo](https://avatars.discourse-cdn.com/v4/letter/f/c68b51/32.png) [@fraccalo](https://community.wolfram.com/u/fraccalo)\
**Post date:** [March 8, 2015, 11:02pm UTC](https://community.wolfram.com/t/how-to-avoid-the-nested-list-in-the-command-table/5056/1 "2015-03-08T23:02:03Z")

</div>

Hi guys,  
using a code like this:

```
Table[{{i, j, k, l}}, {i,1,2}, {j,1,2}, {k,1,2}, {l,1,2}] // MatrixForm

```

I obtain a nested list:

 ![Schermata2015-03-08alle23.40.08.png](https://global.discourse-cdn.com/wolfram/original/3X/7/9/79b9c8f282962bb2e8db0fbfdfede4c915b9b5bf.png)

Instead of that, I’d like to obtain just a simple array composed of 16 vectors 1x4 like this one:

 ![Schermata2015-03-08alle23.46.59.png](https://global.discourse-cdn.com/wolfram/original/3X/f/0/f07781b8de35d9feaddb5a78abd049f54f10fef0.png)

I need that because i have to do those calculations:

```
vectors = 
  Table[KroneckerProduct[misure[[i]], misure[[j]], misure[[k]], 
    misure[[l]]], {i, 1, 2}, {j, 1, 2}, {k, 1, 2}, {l, 1, 2}];

aspectedvalue = 
  Table[FullSimplify[
    Abs[KroneckerProduct[misure[[i]], misure[[j]], misure[[k]], 
        misure[[l]]].ConjugateTranspose[\[Psi]]]^2, 
    Assumptions -> {\[Phi] \[Element] Reals}], {i, 1, 2}, {j, 1, 
    2}, {k, 1, 2}, {l, 1, 2}];

```

and I want to obtain a simple output in both.

In the first one, I’d like to obtain a 16x(1x16) array of vectors (misure[[i]] are 1x2 vectors, and the kronecker product of 4 of them gives a 1x16 vector); in the latter I’d like to obtain a 16x1 vector (because those are just scalars.).  
If there isn’t a clever way to do this with table, I’ll use for cycles…

I hope I made myself clear, thank you

---

<div class="post-metadata">

**Author:** ![kayh](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kayh/32/31501_2.png) [@kayh](https://community.wolfram.com/u/kayh)\
**Post date:** [March 8, 2015, 11:48pm UTC](https://community.wolfram.com/t/how-to-avoid-the-nested-list-in-the-command-table/5056/2 "2015-03-08T23:48:41Z")

</div>

How about using Tuples:

```
Tuples[{1, 2}, 4] // MatrixForm

```

---

<div class="post-metadata">

**Author:** ![nma](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@nma](https://community.wolfram.com/u/nma)\
**Post date:** [March 9, 2015, 3:44am UTC](https://community.wolfram.com/t/how-to-avoid-the-nested-list-in-the-command-table/5056/3 "2015-03-09T03:44:18Z")

</div>

This is a job for `ArrayReshape`

```
r = Table[{i, j, k, L0},
  {i, 1, 2},
  {j, 1, 2},
  {k, 1, 2},
  {L0, 1, 2}]
  (ArrayReshape[r, {16, 4}]) // MatrixForm

```

 ![5848Untitled.png](https://global.discourse-cdn.com/wolfram/original/2X/1/1e6fade1167030cd8e0dad548b1799c953436ae6.png)

ps. in this specific case, as was mentioned above, `Tuples[{1, 2}, 4]` will do the same and much easier.

---

<div class="post-metadata">

**Author:** ![karstenpotrick1](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/karstenpotrick1/32/41421_2.png) [@karstenpotrick1](https://community.wolfram.com/u/karstenpotrick1)\
**Post date:** [March 9, 2015, 1:38pm UTC](https://community.wolfram.com/t/how-to-avoid-the-nested-list-in-the-command-table/5056/4 "2015-03-09T13:38:34Z")

</div>

You can “unnest” it using `Flatten`:

```
Table[{{i, j, k, l}}, {i, 1, 2}, {j, 1, 2}, {k, 1, 2}, {l, 1, 2}] ~Flatten~ 4 // MatrixForm

```

 ![vector.png](https://global.discourse-cdn.com/wolfram/original/2X/1/12843ff9c1886b356f820f333709e804506b9a09.png)

---

<div class="post-metadata">

**Author:** ![kubapod](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kubapod/32/18662_2.png) [@kubapod](https://community.wolfram.com/u/kubapod)\
**Post date:** [March 9, 2015, 2:00pm UTC](https://community.wolfram.com/t/how-to-avoid-the-nested-list-in-the-command-table/5056/5 "2015-03-09T14:00:38Z")

</div>

just to show different approach:

```
table // Cases[#, _?VectorQ, {-2}] &

```

---

<div class="post-metadata">

**Author:** ![fraccalo](https://avatars.discourse-cdn.com/v4/letter/f/c68b51/32.png) [@fraccalo](https://community.wolfram.com/u/fraccalo)\
**Post date:** [March 9, 2015, 9:18pm UTC](https://community.wolfram.com/t/how-to-avoid-the-nested-list-in-the-command-table/5056/6 "2015-03-09T21:18:49Z")

</div>

Thank you all guys, at this moment I have done in this way:

```
Table[KroneckerProduct[misure[[i]], misure[[j]], misure[[k]], 
   misure[[l]]], {i, 1, 2}, {j, 1, 2}, {k, 1, 2}, {l, 1, 2}];
Flatten[%];
linearvectors = ArrayReshape[%, {16, 1, 16}];

```

Is there any way to put all those commands in one line? (I’m sorry for those idiot questions, this is my second day using mathematica and I had no time to read a manual ☹ )

---

<div class="post-metadata">

**Author:** ![kubapod](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kubapod/32/18662_2.png) [@kubapod](https://community.wolfram.com/u/kubapod)\
**Post date:** [March 9, 2015, 10:34pm UTC](https://community.wolfram.com/t/how-to-avoid-the-nested-list-in-the-command-table/5056/7 "2015-03-09T22:34:52Z")

</div>

linearvectors = KroneckerProduct @@@ (misure[[#]] & /@ Tuples[{1, 2}, 4])
