It’s common for lists that come out of one computation to have to be rearranged before going into another computation. For example, one might have a list of pairs, and need to convert it into a pair of lists, or vice versa.

Transpose[{{1,2},{3,4},{5,6},{7,8},{9,10}}]

Transpose[{{1,3,5,7,9},{2,4,6,8,10}}]

Thread is a closely related operation, often useful for generating input to Graph.

Thread[{1,3,5,7,9}->{2,4,6,8,10}]

Partition takes a list, and partitions it into blocks of a specified size.

Partition[Range[12],3]

Grid[Partition[Characters["An array of text made in the Wolfram Language"],9],Frame->All]

If you don’t tell it otherwise, Partition breaks a list up into non-overlapping blocks. But you can also tell it to break the list into blocks that have some specified offset.

Partition[Range[10],3,1]

Grid[Partition[Characters["Wolfram Language"],12,1],Frame->All]

Grid[Partition[Characters["Wolfram Language"],12,2],Frame->All]

Partition takes a list and breaks it into sublists. Flatten “flattens out” sublists.

IntegerDigits/@Range[20]

Flatten[IntegerDigits/@Range[20]]

ListLinePlot[Flatten[IntegerDigits/@Range[20]]]

Flatten will normally flatten out all levels of lists. But quite often you only want to flatten, say, one level of lists. This makes a 4×4 table in which each element is itself a list.

Table[IntegerDigits[i^j],{i,4},{j,4}]

Flatten[Table[IntegerDigits[i^j],{i,4},{j,4}]]

Flatten[Table[IntegerDigits[i^j],{i,4},{j,4}],1]

ArrayFlatten is a generalization of Flatten, which takes arrays of arrays, and flattens them into individual arrays.

NestList[{{#,0},{#,#}}&,{{1}},2]

NestList[ArrayFlatten[{{#,0},{#,#}}]&,{{1}},2]

ArrayPlot/@NestList[ArrayFlatten[{{#,0},{#,#}}]&,{{1}},4]

ArrayPlot[Nest[ArrayFlatten[{{#,0},{#,#}}]&,{{1}},8]]

There are many other ways to rearrange lists. For example, Split splits a list into runs of identical elements.

Split[{1,1,1,2,2,1,1,3,1,1,1,2}]

Gather, on the other hand, gathers identical elements together, wherever they appear.

Gather[{1,1,1,2,2,1,1,3,1,1,1,2}]

GatherBy gathers elements according to the result of applying a function to them. Here it’s using LetterQ, so that it gathers separately letters and non-letters.

GatherBy[Characters["It's true that 2+2 is equal to 4!"],LetterQ]

SortBy sorts according to the result of applying a function.

Sort[Table[IntegerDigits[2^n],{n,10}]]

SortBy[Table[IntegerDigits[2^n],{n,10}],First]

Sort sorts a list in order. Union also removes any repeated elements.

Union[{1,9,5,3,1,4,3,1,3,3,5,3,9}]

You can use Union to find the “union” of elements that appear in any of several lists.

Union[{2,1,3,7,9},{4,5,1,2,3,3},{3,1,2,8,5}]

Intersection[{2,1,3,7,9},{4,5,1,2,3,3},{3,1,2,8}]

Complement[{4,5,1,2,3,3},{3,1,2,8}]

Union[Alphabet["English"],Alphabet["Swedish"],Alphabet["Turkish"]]

Complement[Alphabet["Swedish"],Alphabet["English"]]

Another of the many functions you can apply to lists is Riffle, which inserts things between successive elements of a list.

Riffle[{1,2,3,4,5},x]

Riffle[Characters["WOLFRAM"],"--"]

StringJoin[Riffle[Characters["WOLFRAM"],"--"]]

Functions like Partition let you take a list and break it into sublists. Sometimes you’ll instead just want to start from a collection of possible elements, and form lists from them.

Permutations gives all possible orderings, or permutations, of a list.

Permutations[{Red,Green,Blue}]

Subsets[{Red,Green,Blue}]

Tuples takes a list of elements, and generates all possible combinations of a given number of those elements.

Tuples[{Red,Green},3]

RandomChoice lets you make a random choice from a list of elements.

RandomChoice[{Red,Green,Blue}]

RandomChoice[{Red,Green,Blue},20]

RandomChoice[{Red,Green,Blue},{5,3}]

RandomSample picks a random sample of elements from a list, never picking any particular element more than once.

RandomSample[Range[100],20]

RandomSample[Range[10]]

Use Thread to make a list of rules with each letter of the alphabet going to its position in the alphabet. »

Make a 4×6 grid of the first 24 letters of the alphabet. »

Make a grid of the digits in 2^1000, with 50 digits per row, and put frames around everything. »

Make a grid of the first 400 characters in the Wikipedia article for “computers”, with 20 characters per row, and frames around everything. »

Make a line plot of the flattened list of the digits from the numbers from 0 to 200 (Champernowne sequence). »

Make 4 steps in the “Menger sponge” analog of the fractal Sierpinski pattern from the text, but with a “kernel” of the form {{#,#,#},{#,0,#},{#,#,#}}. »

Find Pythagorean triples involving only integers by selecting {x,y,Sqrt[x^2+y^2]} with x and y up to 20. »

Find the lengths of the longest sequences of identical digits in 2^n for n up to 100. »

Take the names of integers up to 100 and gather them into sublists according to their first letters. »

Sort the first 50 words in WordList[ ] by their last letters. »

Make a list of the first 20 squares, sorted by their first digits. »

Sort integers up to 20 by the length of their names in English. »

Get a random sample of 20 words from WordList[ ], and gather them into sublists by length. »

Find letters that appear in Ukrainian but not Russian. »

Use Intersection to find numbers that appear both among the first 100 squares and cubes. »

Find the list of countries that are in both NATO and the G8. »

Make a grid in which all possible permutations of the numbers 1 through 4 appear as successive columns. »

Make a list of all the different strings that can be obtained by permuting the characters in “hello”. »

Make an array plot of the sequence of possible 5-tuples of 0 and 1. »

Generate a list of 10 random sequences of 5 letters. »

Find a simpler form for Flatten[Table[{i,j,k},{i,2},{j,2},{k,2}],2]. »

Make an array plot from the numbers of the letters in the first 1000 characters in the Wikipedia article on “computers”, with 30 letters per row. »

Gather integers up to 30 into lists based on their values modulo 3. »

Gather the first 50 powers of 2 according to their last digits. »

Make a line plot of the result of sorting the numbers from −10 to +10 by their absolute values. »

Make a line plot of the list of the first 200 squares, sorted by their first digits. »

Make a line plot of integers up to 200 sorted by the lengths of their names in English. »

Get a random sample of 25 words from WordList[]. »

Riffle periods into the string “UNCLE” to make “U.N.C.L.E.”. »

Find letters that appear in Swedish or Polish but not English. »

Find countries that are in the World Health Organization but not the United Nations. »

Make a list of all pair-wise blends of red, green and blue. »

Make a list of array plots, each 50 rows high and with image size 50, of successive possible 8-tuples of 0 and 1. »

Generate 1000 random sequences of 4 letters, and make a list of ones that appear in WordList[ ].  »

What does Partition do if the blocks don’t fit perfectly?

If it’s not told otherwise, it’ll only include complete blocks, so it’ll just drop elements that appear only in incomplete blocks. However, if you say for example Partition[list,UpTo[4]], it’ll make blocks up to length 4, with the last block being shorter if necessary.

Tech Note