How to | Get Elements of Lists
Lists are very important structures in the Wolfram Language. Lists allow you to treat any kind of collection of objects as a single entity. Sometimes you need to pick out or extract individual elements or groups of elements from a list.
Set up a list of the first 10 squares (stored as v):
v = Range[10] ^ 2Use Part to pick the third element of the list:
Part[v, 3]Or use [[...]] (the short form notation for Part):
v[[3]]Combine ;; (the short form for Span) with Part to pick out the elements from 1 to 5:
v[[1 ;; 5]]Pick out elements 5 through 8 of the list:
v[[5 ;; 8]]Pick the last 7 elements (negative indices count from the end):
v[[-7 ;; ]]A matrix in the Wolfram Language is a list of lists of the same length. You can pick out elements from a matrix just like you would from a list.
Set up a 5×5 matrix of integers:
m = Partition[Range[25] ^ 2, 5]Use MatrixForm to see it laid out as a matrix:
MatrixForm[m]The first part of the matrix is the first row:
m[[1]]