5 | Operations on Lists |
There are thousands of functions in the Wolfram Language that work with lists.
You can do arithmetic with lists:
{1, 2, 3} + 10
{1, 1, 2}*{1, 2, 3}
Compute the first 10 squares:
Range[10]^2
Plot the first 20 squares:
ListPlot[Range[20]^2]
Sort sorts a list into order:
Sort[{4, 2, 1, 3, 6}]
Length finds how long a list is:
Length[{5, 3, 4, 5, 3, 4, 5}]
Total gives the total from adding up a list:
Total[{1, 1, 2, 2}]
Find the total of the numbers from 1 to 10:
Total[Range[10]]
Count counts the number of times something appears in a list.
Count the number of times a appears in the list:
Count[{a, b, a, a, c, b, a}, a]
It’s 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:
First[{7, 6, 5}]
Pick out the last element:
Last[{7, 6, 5}]
Pick out element number 2:
Part[{7, 6, 5}, 2]
Picking out the first element in a list you’ve sorted is the same as finding the minimum element:
First[Sort[{6, 7, 1, 2, 4, 5}]]
Min[{6, 7, 1, 2, 4, 5}]
If you have a number, like 5671, you can make a list of its digits using IntegerDigits[5671].
Break a number into a list of digits:
IntegerDigits[1988]
Find the last digit:
Last[IntegerDigits[1988]]
Take lets you take a specified number of elements from the beginning of a list.
Take the first 3 elements from a list:
Take[{101, 203, 401, 602, 332, 412}, 3]
Take the first 10 digits of 2 to the power 100:
Take[IntegerDigits[2^100], 10]
Drop drops elements from the beginning of a list.
Drop[{101, 203, 401, 602, 332, 412}, 3]
{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. »
5.2Find the total of the first 10 squares. »
5.3Make a plot of the first 10 squares, starting at 1. »
5.6Make a combined list of the first 5 squares and cubes (numbers raised to the power 3), sorted into order. »
5.7Find the number of digits in 2^128. »
5.8Find the first digit of 2^32. »
5.9Find the first 10 digits in 2^100. »
5.10Find the largest digit that appears in 2^20. »
5.11Find how many zeros appear in the digits of 2^1000. »
5.13Make a line plot of the sequence of digits that appear in 2^128. »
+5.1Make a list of the first 10 multiples of 3. »
+5.3Find the last digit of 2^37. »
+5.4Find the second-to-last digit of 2^32. »
+5.5Find the sum of all the digits of 3^126. »
+5.6Make a pie chart of the sequence of digits that appear in 2^32. »
+5.7Make a list of pie charts for the sequence of digits in 2^20, 2^40, 2^60. »
Can one add lists of different lengths?
Can there be a list with nothing in it?
Yes. {} is a list of length 0, with no elements. It’s 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.