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

In[]:=

v={3,4,56,2}

Out[]=

{3,4,56,2}

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

In[]:=

v[[3]]=-5;

In[]:=

v

Out[]=

{3,4,-5,2}

## 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.

In[]:=

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

Out[]=

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

Get the first element

In[]:=

First[megaList]

Out[]=

3

Get the last

In[]:=

Last[megaList]

Out[]=

9

Take the first 3 elements

In[]:=

Take[megaList,3]

Out[]=

{3,4,2}

List without the first 3 elements

In[]:=

Drop[megaList,3]

Out[]=

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

List without the first element

In[]:=

Rest[megaList]

Out[]=

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

List without the last element

In[]:=

Most[megaList]

Out[]=

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

Find the position where a particular value occurs

In[]:=

Position[megaList,2]

Out[]=

{{3},{7}}

How many times does some value occur

In[]:=

Count[megaList,2]

Out[]=

2

Is some value a member of a list?

In[]:=

MemberQ[megaList,45]

Out[]=

False

Is the list free of some value?

In[]:=

FreeQ[megaList,0]

Out[]=

True

Add an element to the front of the list

In[]:=

Prepend[megaList,42]

Out[]=

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

Add to the end

In[]:=

Append[megaList,42]

Out[]=

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

Insert 1000 at position 5

In[]:=

Insert[megaList,1000,5]

Out[]=

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

Interleave x through the list

In[]:=

Riffle[megaList,x]

Out[]=

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

Delete element at position 5

In[]:=

Delete[megaList,5]

Out[]=

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

Replace element at position 7 with something else like another list

Replace element at position 7 with something else like another list

In[]:=

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

Out[]=

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

Concatenate any number of lists

In[]:=

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

Out[]=

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

Reverse a list

In[]:=

Reverse[Range[10]]

Out[]=

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

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.

In[]:=

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

Out[]=

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

Sort the list

In[]:=

Sort[rands]

Out[]=

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

Let’s partition the random list into pairs

In[]:=

Partition[rands,2]

Out[]=

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

Let’s split it into identical runs of elements

In[]:=

Split[rands]

Out[]=

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

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

In[]:=

Min[megaList]

Out[]=

1

In[]:=

Max[megaList]

Out[]=

9

Get min and max together in one go!

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

## Exercise

Exercise

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