Explore the latest version of An Elementary Introduction to the Wolfram Language »
31Parts of Lists
Part lets you pick out an element of a list.
In[1]:=
Click for copyable input
Out[1]=
[[...]] is an alternative notation.
Use [[2]] to pick out element 2 from a list:
In[2]:=
Click for copyable input
Out[2]=
In[3]:=
Click for copyable input
Out[3]=
You can ask for a list of parts by giving a list of part numbers.
Pick out parts 2, 4 and 5:
In[4]:=
Click for copyable input
Out[4]=
;; lets you ask for a span or sequence of parts.
Pick out parts 2 through 5:
In[5]:=
Click for copyable input
Out[5]=
In[6]:=
Click for copyable input
Out[6]=
Take the last 2 elements from a list:
In[7]:=
Click for copyable input
Out[7]=
Drop the last 2 elements from a list:
In[8]:=
Click for copyable input
Out[8]=
In[9]:=
Click for copyable input
Out[9]=
Pick out the second sublist, corresponding to the second row, in the array:
In[10]:=
Click for copyable input
Out[10]=
This picks out element 1 on row 2:
In[11]:=
Click for copyable input
Out[11]=
Pick out the first column, by getting element 1 on all rows:
In[12]:=
Click for copyable input
Out[12]=
The function Position finds the list of positions at which something appears.
In[13]:=
Click for copyable input
Out[13]=
This gives a list of all positions at which x appears:
In[14]:=
Click for copyable input
Out[14]=
The positions at which a occurs in a list of characters:
In[15]:=
Click for copyable input
Out[15]=
In[16]:=
Click for copyable input
Out[16]=
The function ReplacePart lets you replace parts of a list:
Replace part 3 with x:
In[17]:=
Click for copyable input
Out[17]=
Replace two parts:
In[18]:=
Click for copyable input
Out[18]=
In[19]:=
Click for copyable input
Out[19]=
Nothing just disappears:
In[20]:=
Click for copyable input
Out[20]=
Replace parts 1 and 3 with Nothing:
In[21]:=
Click for copyable input
Out[21]=
Take 50 random words, dropping ones longer than 5 characters, and reversing others:
In[22]:=
Click for copyable input
Out[22]=
Take takes a specified number of elements in a list based on their position. TakeLargest and TakeSmallest take elements based on their size.
Take the 5 largest elements from a list:
In[23]:=
Click for copyable input
Out[23]=
TakeLargestBy and TakeSmallestBy take elements based on applying a function.
In[24]:=
Click for copyable input
Out[24]=
Part[list,n] part n of a list
list[[n]] short notation for part n of a list
list[[{n1,n2,...}]] list of parts n1, n2, ...
list[[n1;;n2]] span (sequence) of parts n1 through n2
list[[m,n]] element from row m, column n of an array
list[[All,n]] all elements in column n
Take[list,n] take the first n elements of a list
TakeLargest[list,n] take the largest n elements of a list
TakeSmallest[list,n] take the smallest n elements of a list
TakeLargestBy[list,f,n] take elements largest by applying f
TakeSmallestBy[list,f,n] take elements smallest by applying f
Position[list,x] all positions of x in list
ReplacePart[list,nx] replace part n of list with x
Nothing a list element that is automatically removed
31.1Find the last 5 digits in 2^1000. »
Expected output:
Out[]=
31.2Pick out letters 10 through 20 in the alphabet. »
Expected output:
Out[]=
31.3Make a list of the letters at even-numbered positions in the alphabet. »
Expected output:
Out[]=
31.4Make a line plot of the second to last digit in the first 100 powers of 12. »
Expected output:
Out[]=
31.5Join lists of the first 20 squares and cubes, and get the 10 smallest elements of the combined list. »
Expected output:
Out[]=
31.6Find the positions of the word software in the Wikipedia entry for computers»
Sample expected output:
Out[]=
31.7Make a histogram of where the letter e occurs in the words in WordList[ ]»
Sample expected output:
Out[]=
31.8Make a list of the first 100 cubes, with every one whose position is a square replaced by Red»
Expected output:
Out[]=
31.9Make a list of the first 100 primes, dropping ones whose first digit is less than 5. »
Expected output:
Out[]=
31.10Make a grid starting with Range[10], then at each of 9 steps randomly removing another element. »
Sample expected output:
Out[]=
31.11Find the longest 10 words in WordList[ ]»
Sample expected output:
Out[]=
31.12Find the 5 longest integer names for integers up to 100. »
Expected output:
Out[]=
Expected output:
Out[]=
Usually list part n or list sub n. The second form (with sub short for subscript) comes from thinking about math and extracting components of vectors.
What happens if one asks for a part of a list that doesnt exist?
One gets a message, and the original computation is returned undone.
Can I just get the first position at which something appears in a list?
  • First and Last are equivalent to [[1]] and [[-1]].
  • In specifying parts, 1;;-1 is equivalent to All.
 
Download Notebook Version
es