Tags: "array", "python", access_time 2-min read

Edit this post on Github

Arrays in Python

Created: November 13, 2018 by [lek-tin]

Last updated: November 13, 2018

Reverse an array

arr = [1, 2, 3, 4, 5, 6, 7, 8]
reversedArray = arr[::-1]
# [8, 7, 6, 5, 4, 3, 2, 1]
arr[3:5] = arr[3:5][::-1]

Slices with step

s[i:j:k] means “slice of s from i to j with step k”. When i and j are absent, the whole sequence is assumed and thus s[::k] means “every k-th item” in the entire sequence.

s = range(20)
# output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

# 3rd item from s:
s[::3]
# output: [0, 3, 6, 9, 12, 15, 18]

# 3rd item from s[2:]:
s[2:]
# output: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
s[2::3]
# output: [2, 5, 8, 11, 14, 17]

# 3rd item from s[5:12]:
s[5:12]
# output: [5, 6, 7, 8, 9, 10, 11]
s[5:12:3]
# output: [5, 8, 11]

# 3rd item from s[:10]:
s[:10]
# output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
s[:10:3]
# output: [0, 3, 6, 9]

Clone a list

newArr = oldArr[:]

Slicing

>>> a = numpy.arange(3)
>>> a
array([0, 1, 2])
>>> b = a[1:3]
>>> b
array([1, 2])
What happens when we modify a and look again at b?

>>> a[2] = 1001
>>> b
array([   1, 1001])