An Elementary Introduction to the Wolfram Language
5Operations on Lists
There are thousands of functions in the Wolfram Language that work with lists.
In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
Compute the first 10 squares:
In[3]:=
Click for copyable input
Out[3]=
Plot the first 20 squares:
In[4]:=
Click for copyable input
Out[4]=
Sort sorts a list into order:
In[5]:=
Click for copyable input
Out[5]=
Length finds how long a list is:
In[6]:=
Click for copyable input
Out[6]=
Total gives the total from adding up a list:
In[7]:=
Click for copyable input
Out[7]=
Find the total of the numbers from 1 to 10:
In[8]:=
Click for copyable input
Out[8]=
Count counts the number of times something appears in a list.
Count the number of times a appears in the list:
In[9]:=
Click for copyable input
Out[9]=
Its often useful to be able to get individual elements of a list. First gives the first element; Last gives the last element. Part gives the element at a particular position.
Pick out the first element of a list:
In[10]:=
Click for copyable input
Out[10]=
Pick out the last element:
In[11]:=
Click for copyable input
Out[11]=
Pick out element number 2:
In[12]:=
Click for copyable input
Out[12]=
Picking out the first element in a list youve sorted is the same as finding the minimum element:
In[13]:=
Click for copyable input
Out[13]=
In[14]:=
Click for copyable input
Out[14]=
Break a number into a list of digits:
In[15]:=
Click for copyable input
Out[15]=
Find the last digit:
In[16]:=
Click for copyable input
Out[16]=
Take lets you take a specified number of elements from the beginning of a list.
Take the first 3 elements from a list:
In[17]:=
Click for copyable input
Out[17]=
Take the first 10 digits of 2 to the power 100:
In[18]:=
Click for copyable input
Out[18]=
Drop drops elements from the beginning of a list.
In[19]:=
Click for copyable input
Out[19]=
{2,3,4}+{5,6,2} arithmetic on lists
Sort[{5,7,1}] sort a list into order
Length[{3,3}] length of a list (number of elements)
Total[{1,1,2}] total of all elements in a list
Count[{3,2,3},3] count occurrences of an element
First[{2,3}] first element in a list
Last[{6,7,8}] last element in a list
Part[{3,1,4},2] particular part of a list, also written as {3, 1, 4}[[2]]
Take[{6,4,3,1},2] take elements from the beginning of a list
Drop[{6,4,3,1},2] drop elements from the beginning of a list
IntegerDigits[1234] list of digits in a number
5.1Make a list of the first 10 squares, in reverse order. »
Expected output:
Out[]=
5.2Find the total of the first 10 squares. »
Expected output:
Out[]=
5.3Make a plot of the first 10 squares, starting at 1. »
Expected output:
Out[]=
5.4Use Sort, Join and Range to create {1, 1, 2, 2, 3, 3, 4, 4}»
Expected output:
Out[]=
5.5Use Range and + to make a list of numbers from 10 to 20, inclusive. »
Expected output:
Out[]=
Expected output:
Out[]=
5.7Find the number of digits in 2^128. »
Expected output:
Out[]=
5.8Find the first digit of 2^32. »
Expected output:
Out[]=
5.9Find the first 10 digits in 2^100. »
Expected output:
Out[]=
5.10Find the largest digit that appears in 2^20. »
Expected output:
Out[]=
5.11Find how many zeros appear in the digits of 2^1000. »
Expected output:
Out[]=
5.12Use Part, Sort and IntegerDigits to find the second-smallest digit in 2^20. »
Expected output:
Out[]=
5.13Make a line plot of the sequence of digits that appear in 2^128. »
Expected output:
Out[]=
5.14Use Take and Drop to get the sequence 11 through 20 from Range[100]»
Expected output:
Out[]=
+5.1Make a list of the first 10 multiples of 3. »
Expected output:
Out[]=
+5.2Make a list of the first 10 squares using only Range and Times»
Expected output:
Out[]=
+5.3Find the last digit of 2^37. »
Expected output:
Out[]=
+5.4Find the second-to-last digit of 2^32. »
Expected output:
Out[]=
+5.5Find the sum of all the digits of 3^126. »
Expected output:
Out[]=
+5.6Make a pie chart of the sequence of digits that appear in 2^32. »
Expected output:
Out[]=
+5.7Make a list of pie charts for the sequence of digits in 2^20, 2^40, 2^60. »
Expected output:
Out[]=
Can one add lists of different lengths?
No. {1, 2}+{1, 2, 3} wont work. {1, 2, 0}+{1, 2, 3} would be fine, if thats what you mean.
Yes. {} is a list of length 0, with no elements. Its usually called the null list or the empty list.
  • IntegerDigits[5671] gives digits in base 10. IntegerDigits[5671, 2] gives digits in base 2. You can use any base you want. FromDigits[{5, 6, 7, 1}] reconstructs a number from its list of digits.
  • Rest[list] gives all the elements of list after the first one. Most[list] gives all elements other than the last one.
 
Download Notebook Version