Search
Collections, Numpy and Slicing

Python Collections, Numpy Arrays, and Slicing

Below, we explore some basic Python programs for demonstrating use of Python's built-in data types, focusing on collections and numpy arrays. We also introduce the concept of slicing, which allows us to quickly and cleanly extract subset of sequence and array types.

Collections

Python has built-in support for "collection" data types - these are complex data types that hold other collections of other data. Python provides three basic collection types:

TypeDescriptionExamples
String Collection of characters, indicated by single or double quotes
  • "this is a string"
  • 'this is also a string'
  • 'ABC-123'
List Collection of 'mutable' objects (they can be changed), indicated by brackets ([ ])
  • [1,2,3]
  • [ 'a', 32, 14.7 ]'
  • [ 'A','list','of','strings' ]
  • ["a list containing", [ "another list" ] ]
Tuple Collection of 'unmutable' objects (they can NOT be changed), indicated by parentheses (( ))
  • (1,2,3)
  • ( 'a', 32, 14.7 )'
  • [ 'A','tuple','of','strings' ]
  • ["a tuple containing", ( "another tuple" ) )
Dictionary Collection of key:value pairs, where keys are used to look up values, indicated by braces ({ })
  • {'name':'John', 'age':56}

Additionally, numpy provides array and matrix types that we will use extensively.

Making Collections

Collections can be created in multiple ways...

import numpy as np

# make a string
mystr = "this is a string"
print("string:", mystr)# make a list

# make a list
mylist = [1,2,3,4,5]
print("list:", mylist)

# make a tuple
mytuple = ("this is a tuple", 4, ("a tuple within a tuple"))
print("tuple:", mytuple)

# make a few numpy arrays
mynp_zeros = np.zeros(10)   # array of ten zeros
print("np.zeros():", mynp_zeros)
mynp_ones = np.ones(10)   # array of ten ones
print("np.ones():", mynp_ones)
mynp_range = np.arange(0,5,0.5)  # range of values, starting at 0, ending at 4.5, in increments of 0.5
print("np.arange():", mynp_range)
mynp_range2 = np.linspace(0,5,11)  # evenly spaced range of 10 values, starting at 0, ending at 5
print("np.linspace():", mynp_range2)
mynp_matrix = np.zeros((2,3), dtype=int)  # 2D matrix of integers, filled with zeros
print("2x3 matrix:\n", mynp_matrix)

mydictionary = {'name':'John', 'age':10 } # dictionary (key:value pairs
print("dictionary:", mydictionary)
string: this is a string
list: [1, 2, 3, 4, 5]
tuple: ('this is a tuple', 4, 'a tuple within a tuple')
np.zeros(): [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
np.ones(): [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
np.arange(): [0.  0.5 1.  1.5 2.  2.5 3.  3.5 4.  4.5]
np.linspace(): [0.  0.5 1.  1.5 2.  2.5 3.  3.5 4.  4.5 5. ]
2x3 matrix:
 [[0 0 0]
 [0 0 0]]
dictionary: {'name': 'John', 'age': 10}

Counting, appending, inserting, deleting items in a List

a = [1,2,3,4,5,6]  # 'a' is a list, but it works pretty much the same with numpy arrays

print( "length of a: {}".format(len(a)))    # len(collection) returns the count of items in the collection

# append list
a = a + [7,8]  # '+' returns a new object, so we have to assign a variable to it
print("After +: ", a)

# append single item 
a.append(10)    # .append() operates on an existing object, so no assignment necessary
print("After append(): ", a)

# insert an item (0) at a specific location (index 5) - remember indexes are zero-based
a.insert(5,0)
print("After insert():", a)

# remove a particular item in the collection
a.remove(0)
print("After remove():", a)

# delete an item at a particular index location
del(a[2])
print("After del():", a)

# clear out an array
a.clear()   # or a=[]
print("After clear():", a)
length of a: 6
After +:  [1, 2, 3, 4, 5, 6, 7, 8]
After append():  [1, 2, 3, 4, 5, 6, 7, 8, 10]
After insert(): [1, 2, 3, 4, 5, 0, 6, 7, 8, 10]
After remove(): [1, 2, 3, 4, 5, 6, 7, 8, 10]
After del(): [1, 2, 4, 5, 6, 7, 8, 10]
After clear(): []

Counting, appending, inserting, deleting, reshaping items in a numpy array

These are but a few of the ways - see the numpy docs at https://numpy.org for more information.

import numpy as np

a = np.array([1,2,3,4,5,6])  # 'a' now a numpy array

print( "length of a: {}".format(len(a)))    # len(collection) returns the count of items in the collection

# append list
a = np.concatenate((a,[7,8]))  # returns a new object, so we have to assign a variable to it
print("After +: ", a)

# append single item 
a = np.append(a,[10])    # np.append() returns a new oject, assignment necessary
print("After append(): ", a)

# insert an item (0) at a specific location (index 5) - remember indexes are zero-based
a = np.insert(a,5,0)
print("After insert():", a)

# delete an item at a particular index location
a  = np.delete(a,2)
print("After delete():", a)

# reshape an array (returns new array (or view))
a = a.reshape((3,3))
print("After reshape():", a)
length of a: 6
After +:  [1 2 3 4 5 6 7 8]
After append():  [ 1  2  3  4  5  6  7  8 10]
After insert(): [ 1  2  3  4  5  0  6  7  8 10]
After delete(): [ 1  2  4  5  0  6  7  8 10]
After reshape(): [[ 1  2  4]
 [ 5  0  6]
 [ 7  8 10]]

Dictionaries

Dictionaries are a slightly different, but often useful, collection type. Instead of accessing elements by location (index), we access them by providing a key, which can be a string, number or about anything else. Dictionaries are useful when it is convenient to store information by keyword lookup.

In the example below, we use a dictionary to store information about an satellite image, but the concept can be extended to about anything.

# make an empty dictionary
imageInfo = {}   # 'a' is now an empty dictionary

# add some key:value pairs to 
imageInfo['location'] = 'Oregon'
imageInfo['center'] = (45.7,132.34)
imageInfo['width'] = 800
imageInfo['height'] = 800
imageInfo['date'] = '12/12/2109'

print(imageInfo)
print( "length: {}".format(len(imageInfo)))    # len(collection) returns the count of items in the collection

# use key to 'look up' elements
print(imageInfo['center'])
print(imageInfo['date'])
{'location': 'Oregon', 'center': (45.7, 132.34), 'width': 800, 'height': 800, 'date': '12/12/2109'}
length: 5
(45.7, 132.34)
12/12/2109

Accessing items in a Collection

For all of the Collections described above, accessing items in the collection is accomplished using the [ ] operators as follows (remember, indexes are zero-based):

import numpy as np

mylist = [1,2,3,4]
mytuple = (1,2,3,4)
myarray = np.array([1,2,3,4])
mymatrix = np.arange(0,9).reshape(3,3)
mydict = { "key1":"value1", "key2":"value2" }

print(mylist[2])
print(mytuple[2])
print(myarray[2])
print(mymatrix[2,2])
print(mydict['key2'])
3
3
3
8
value2

Slicing

Slicing is an extremely powerful and useful way to extract subsets of elements from list, tuples, numpy arrays, and numpy matrices, and is supported by many additional libraries as well. Slicing builds on the bracket accessor operator ([ ]) notation, and is best demonstrated by example:

import numpy as np

mylist = [0,1,2,3,4]
myarray = np.array([0,1,2,3,4,5,6,7,8])
mymatrix = mymatrix.reshape(3,3)
print("mylist:", mylist)
print("myarray:", myarray)
print("mymatrix:\n", mymatrix)

# get a subset - [start:end]   NOTE: up to BUT NOT INCLUDING end!
print( "mylist[1:3]:", mylist[1:3])  # start at index 1, end at index 2
print( "myarray[1:3]:", myarray[1:3])

# for 2D matrices, we need to specify row and columns
print( "mymatrix[0:2,1:3]:\n", mymatrix[0:2,1:3])  # rows 1 and 2, column 0

# you can "default" to start or end
print( "mylist[:3]:", mylist[:3])  # start at beginning, end at index 2
print( "myarray[2:]:", myarray[2:])  # start at index 2, end at end
print( "mymatrix[:2,2:]:\n", mymatrix[:2,2:])  # rows 0 and 1, last oolumn

# ':'' by itself is "all"
print( "myarray[:]:", myarray[:])  # all items
print( "mymatrix[:,0:2]:\n", mymatrix[:,0:2])  # all rows, columns 0-1
print( "mymatrix[1,:]:\n", mymatrix[1:2,:])  # row 1, all columns,
mylist: [0, 1, 2, 3, 4]
myarray: [0 1 2 3 4 5 6 7 8]
mymatrix:
 [[0 1 2]
 [3 4 5]
 [6 7 8]]
mylist[1:3]: [1, 2]
myarray[1:3]: [1 2]
mymatrix[0:2,1:3]:
 [[1 2]
 [4 5]]
mylist[:3]: [0, 1, 2]
myarray[2:]: [2 3 4 5 6 7 8]
mymatrix[:2,2:]:
 [[2]
 [5]]
myarray[:]: [0 1 2 3 4 5 6 7 8]
mymatrix[:,0:2]:
 [[0 1]
 [3 4]
 [6 7]]
mymatrix[1,:]:
 [[3 4 5]]