Programming
Bash
Arrays
Regex - grep
Strings
C
Arrays
Regex - POSIX
Strings
C++
Arrays
Bimaps
Functions
Maps
Regex
Strings
Go
Arrays
Maps
Strings
Java
Arrays
Regex
Strings
JavaScript
Arrays
Functions
Objects
Regex
Strings
Perl
Arrays
Regex
Strings
Python
Arrays
Initialise
List comprehension
Lambda functions
Insert
Overwrite
Count
Retrieve by index
Remove by index
Slice
Iterate
Search
Sort
Use as stack
Use as FIFO
Evaluation
Compare
Functions
Regex
Strings
Python Arrays
Initialise
Fixed contents n/a a=[1,2,3,4,5]
1 2 3 4 5
Repeat value 'e' 'n' times e=3
n=5
a=[e]*n
3 3 3 3 3
Create using list comprehension
Values in a range, e.g. m..n (inclusive) m=1
n=5
a = range(m,n+1)
1 2 3 4 5
Values of a function, e.g. x*x a=[1,2,3,4,5]
b=[x*x for x in a]
1 4 9 16 25
Values satisfying a condition, e.g. >n a=[1,2,3,4,5]
n=2
b=[x for x in a if x > n]
3 4 5
Array of pairs a=[1,2,3,4,5]
b=[(x,2*x) for x in a]
(1, 2) (2, 4) (3, 6) (4, 8) (5, 10)
Array of triples a=[1,2,3,4,5]
b=[(x,2*x,3*x) for x in a]
(1, 2, 3) (2, 4, 6) (3, 6, 9) (4, 8, 12) (5, 10, 15)
Create using lambda functions
Values of a function, e.g. x*x*x def cube(x): return x * x * x
a=map(cube, range(1,6))
1 8 27 64 125
Values satisfying a condition, e.g. even def even(x): return x % 2 == 0
a=filter(even, range(1,6))
2 4
Insert
Prepend element 'e' a=[2,3,4]
e=1;
a.insert(0,e)
1 2 3 4
Append element 'e' a=[1,2,3]
e=4;
a.append(e)
1 2 3 4
Insert element 'e' at index 'n' a=[1,2,3,4,5]
e=666
n=3
a.insert(n,e)
1 2 3 666 4 5
Prepend array 'b' a=[1,2,3,4,5]
b=[10,20,30]
a=b+a
10 20 30 1 2 3 4 5
Append array 'b' a=[1,2,3,4,5]
b=[10,20,30]
a=a+b
1 2 3 4 5 10 20 30
Insert array 'b' at index 'n' a=[1,2,3,4,5]
b=[10,20,30]
n=3;
a=a[0:n]+b+a[n:]
1 2 3 10 20 30 4 5
Concatenate array 'a' and array 'b' a=[1,2,3]
b=[4,5,6]
c=a+b
1 2 3 4 5 6
Insert element 'e' at indices in 'b' a=[1,2,3,4,5]
b=[0,2,4]
e=666
for n in reversed(b):
  a.insert(n,e)
666 1 2 666 3 4 666 5
Insert element 'e' 'n' times at index 'm' a=[1,2,3]
n=3
m=1
e=666
a=a[0:m]+([e]*n)+a[m:]
1 666 666 666 2 3
Overwrite
Overwrite first element a=[1,2,3]
a[0]=666
666 2 3
Overwrite last element a=[1,2,3]
a[-1]=666
1 2 666
Overwrite element at index 'n' a=[1,2,3]
n=1
a[n]=666
1 666 3
Overwrite elements from index 'n' (inclusive) a=[1,2,3,4,5,6]
b=[7,8,9]
n=2
a[n:n+len(b)]=b
1 2 7 8 9 6
Overwrite all elements a=[1,2,3,4,5,6]
b=[7,8,9]
a[:]=b
7 8 9
Count
Length of array a=[1,2,3]
n=len(a)
3
Number of element 'e' in array 'a' a=[1,2,2,3,3,3]
e=3
n=a.count(e)
3
Maximum element in array a=[1,2,300,4,5]
n=max(a)
300
Minimum element in array a=[10,20,3,40,50]
n=min(a)
3
Sum of elements in array a=[10,20,30,40]
n=sum(a)
100
Product of elements in array a=[1,2,3,4,5]
n=reduce(mul, a)
120
Retrieve by index
Retrieve first element a=[1,2,3]
e=a[0]
1
Retrieve last element a=[1,2,3]
e=a[-1]
3
Retrieve element 'n' a=[1,2,3]
n=2
e=a[n]
3
Retrieve out-of-bounds element 'n' a=[1,2,3]
n=4
try:
  e=a[n]
except:
  e="out of bounds"
out of bounds
Remove by index
Remove element at index 'n' a=[1,2,3,4,5]
n=2;
del a[n]
1 2 4 5
Remove elements in index range 'm' to 'n' (inclusive) a=[1,2,3,4,5]
m=1;
n=3;
del a[m:n+1]
1 5
Remove elements from index 'n' onwards (inclusive) a=[1,2,3,4,5]
n=3;
del a[n:]
1 2 3
Slice
Slice from index 'm' to 'n' (inclusive) a=[1,2,3,4,5]
m=1
n=3
a=a[m:n+1]
2 3 4
Slice from index 'n' onwards (inclusive) a=[1,2,3,4,5]
n=2
a=a[n:]
3 4 5
Slice up to index 'n' (inclusive) a=[1,2,3,4,5]
n=3
a=a[:n+1]
1 2 3 4
Slice every 'n'th index a=[1,2,3,4,5]
n=2
a=a[::n]
1 3 5
Slice up to 'n' elements from end a=[1,2,3,4,5]
n=2
a=a[:-n]
1 2 3
Iterate
Iterate over array 'a' a=[1,2,3,4,5]
b=[]
for x in a:
  b.append(x*2)
2 4 6 8 10
Iterate over array 'a' and 'b' in lockstep import itertools
a=[1,3,5]
b=[2,4,6]
c=[]
for x,y in itertools.izip(a, b):
  c.append(x)
  c.append(y)
1 2 3 4 5 6
Search
Test element 'e' 'is in' array a=[1,2,3,4,5]
e=3;
t=(e in a)
1
Find index of unique element 'e' a=[1,2,3,4,5]
e=3;
n=a.index(e)
2
Find index of first element 'e' a=[1,2,666,3,4,666,5]
e=666;
n=a.index(e)
2
Sort
Sort in ascending order a=[5,1,4,3,2]
a.sort()
1 2 3 4 5
Sort in descending order a=[5,1,4,3,2]
a.sort()
a.reverse()
5 4 3 2 1
Reverse a=[1,2,3,4,5]
a.reverse()
5 4 3 2 1
Use as a stack
Push element 'e' a=[1,2,3,4,5]
e=666;
a.append(e)
1 2 3 4 5 666
Pop next element a=[1,2,3,4,5]
a.pop()
1 2 3 4
Retrieve next element a=[1,2,3,4,5]
e=a[-1]
5
Use as a FIFO
Add element 'e' a=[1,2,3,4,5]
e=666;
a.append(e)
1 2 3 4 5 666
Remove next element a=[1,2,3,4,5]
a.pop(0)
2 3 4 5
Retrieve next element a=[1,2,3,4,5]
e=a[0]
1
Evaluation
Empty array a=[]
t=0
if a: t=1
0
Non-empty array a=[1,2,3,4,5]
t=0
if a: t=1
1
Compare
Test for equality (true) a=[1,2,3,4,5]
b=[1,2,3,4,5]
t=(a==b)
1
Test for equality (false) a=[1,2,3,4,5]
b=[1,2,3,4,50]
t=(a==b)
0