Mathematica - turning two lists into an array

In summary, to turn lists into an array in Mathematica, use the command ArrayFlatten[{{list1},{list2}}]. You can turn any number of lists into an array by adding additional lists within the curly braces. If the lists have different lengths, Mathematica will automatically pad the shorter lists with Null values. To access elements in an array, use the standard Part notation or the Span operator. To convert an array back into a list, use the Flatten command with the desired level of flattening.
  • #1
tomrob123
1
0
Hi

I need to turn two lists in the form {a1,a2,a3...} {b1,b2,b3...}
into {{a1,b1},{a2,b2},{a3,b3}..}

I've looked everywhere on the Wolfram website and the best i found was:

Riffle[{list1},{list2}] which gives me {a1,b1,a2,b2,a3,b3}

does anyone know the function to use?

Thanks
 
Physics news on Phys.org
  • #2
Hi,
Maybe this:
Transpose[{{a1, a2, a3}, {b1, b2, b3}}]
 
  • #3
Equivalently, {{a1, a2, a3}, {b1, b2, b3}}TR. (The transpose symbol comes from hitting Esc, tr, Esc.)
 

FAQ: Mathematica - turning two lists into an array

1. How can I turn two lists into an array using Mathematica?

To turn two lists, list1 and list2, into an array in Mathematica, you can use the command ArrayFlatten[{{list1},{list2}}]. This will create a 2D array with list1 as the first row and list2 as the second row.

2. Can I turn more than two lists into an array using Mathematica?

Yes, you can turn any number of lists into an array using the ArrayFlatten command. Simply add additional lists within the curly braces, separated by commas, as shown in the previous answer.

3. What if my lists have different lengths?

If your lists have different lengths, Mathematica will automatically pad the shorter lists with Null values to create a rectangular array. You can also use the ArrayPad command to specify a different padding value.

4. How do I access elements in an array created by Mathematica?

To access elements in an array, you can use the standard Part notation, which uses double brackets, e.g. array[[i,j]] to access the element in the ith row and jth column. You can also use the Span operator, e.g. array[[1;;3,2]] to access elements from the first to third rows in the second column.

5. Can I convert an array back into a list in Mathematica?

Yes, you can convert an array back into a list using the Flatten command. Simply specify the level of flattening as the second argument, e.g. Flatten[array, 1] will flatten the array by one level and return a list of the elements in the array.

Similar threads

Replies
1
Views
2K
Replies
2
Views
2K
Replies
8
Views
5K
Replies
8
Views
1K
Replies
3
Views
2K
Back
Top