Today is our last day on lists. The intention of the blog series is not to teach you the entirety of Wolfram language, that is already partly done in books like Stephen Wolfram’s book available for free online and in the massive documentation containing more than 100,000 examples. My goal is to give you thinking tools that will enable you quickly start reading and writing programs in Wolfram language. Let’s dive in!

One practical operation is mutating lists

v={3,4,56,2}

In[]:=

{3,4,56,2}

Out[]=

v is a variable and the = equal sign is the immediate assignment operator. As we will see later there are multiple assignment operators in Wolfram Language.

Let’s change the 3rd element to -5

v[[3]]=-5;

In[]:=

v

In[]:=

{3,4,-5,2}

Out[]=

## List FU!

List FU!

List are so important that I found it necessary to create this special section I call List FU, this is a kind of Kung FU but with lists. In this section we will see a set of functions that operate on lists that are indispensable when programming in Wolfram Language. It will be a good thing to go the the documentation and read in detail about each of the functions used here.

Since we now know about variables lets create a list of random numbers and perform all our List FU on it.

megaList={3,4,2,6,8,4,2,7,5,6,1,9}

In[]:=

{3,4,2,6,8,4,2,7,5,6,1,9}

Out[]=

Get the first element

First[megaList]

In[]:=

3

Out[]=

Get the last

Last[megaList]

In[]:=

9

Out[]=

Take the first 3 elements

Take[megaList,3]

In[]:=

{3,4,2}

Out[]=

List without the first 3 elements

Drop[megaList,3]

In[]:=

{6,8,4,2,7,5,6,1,9}

Out[]=

List without the first element

Rest[megaList]

In[]:=

{4,2,6,8,4,2,7,5,6,1,9}

Out[]=

List without the last element

Most[megaList]

In[]:=

{3,4,2,6,8,4,2,7,5,6,1}

Out[]=

Find the position where a particular value occurs

Position[megaList,2]

In[]:=

{{3},{7}}

Out[]=

How many times does some value occur

Count[megaList,2]

In[]:=

2

Out[]=

Is some value a member of a list?

MemberQ[megaList,45]

In[]:=

False

Out[]=

Is the list free of some value?

FreeQ[megaList,0]

In[]:=

True

Out[]=

Add an element to the front of the list

Prepend[megaList,42]

In[]:=

{42,3,4,2,6,8,4,2,7,5,6,1,9}

Out[]=

Add to the end

Append[megaList,42]

In[]:=

{3,4,2,6,8,4,2,7,5,6,1,9,42}

Out[]=

Insert 1000 at position 5

Insert[megaList,1000,5]

In[]:=

{3,4,2,6,1000,8,4,2,7,5,6,1,9}

Out[]=

Interleave x through the list

Riffle[megaList,x]

In[]:=

{3,x,4,x,2,x,6,x,8,x,4,x,2,x,7,x,5,x,6,x,1,x,9}

Out[]=

Delete element at position 5

Delete[megaList,5]

In[]:=

{3,4,2,6,4,2,7,5,6,1,9}

Out[]=

Replace element at position 7 with something else like another list

Replace element at position 7 with something else like another list

ReplacePart[megaList,7{a,b,c}]

In[]:=

{3,4,2,6,8,4,{a,b,c},7,5,6,1,9}

Out[]=

Concatenate any number of lists

Join[Range[5],{x,y,z},RandomInteger[10,10]]

In[]:=

{1,2,3,4,5,x,y,z,9,3,3,3,0,3,4,1,3,0}

Out[]=

Reverse a list

Reverse[Range[10]]

In[]:=

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

Out[]=

Have you noticed how the words we use to describe the operations are similar to the function names that actually perform the operation? Wolfram Language has well named function enabling you to build your computation thinking skills very fast till coding is almost like speaking a natural language.

rands=RandomInteger[10,10](*generate10randomintegerswithinrange10*)

In[]:=

{1,2,2,3,10,6,9,3,3,2}

Out[]=

Sort the list

Sort[rands]

In[]:=

{1,2,2,2,3,3,3,6,9,10}

Out[]=

Let’s partition the random list into pairs

Partition[rands,2]

In[]:=

{{1,2},{2,3},{10,6},{9,3},{3,2}}

Out[]=

Let’s split it into identical runs of elements

Split[rands]

In[]:=

{{1},{2,2},{3},{10},{6},{9},{3,3},{2}}

Out[]=

Let’s not forget finding the minimum element and the maximum element

Min[megaList]

In[]:=

1

Out[]=

Max[megaList]

In[]:=

9

Out[]=

Get min and max together in one go!

MinMax[megaList]

In[]:=

{1,9}

Out[]=

One very popular function you will see in Wolfram Language programs is the flatten function and it does what it’s names says, it flattens nested stuff. lets see it flatten something

{{a},{b,{c}},{d}}

In[]:=

{{a},{b,{c}},{d}}

Out[]=

Flatten[{{a},{b,{c}},{d}}](*totalflattening*)

In[]:=

{a,b,c,d}

Out[]=

Flatten[{{a},{b,{c}},{d}},1](*flattenonlyatlevel1*)

In[]:=

{a,b,{c},d}

Out[]=

## Exercise

Exercise

1. Go to the documentation and read all you can about the functions we have dealt with in this section.