my notes: Python Data Structures

Cheryl
4 min readJan 24, 2020

--

“Where then do I look for good and evil? Not to uncontrollable externals, but within myself to the choices that are my own …” — EPICTETUS

Data structures in Python

Data type does not have to be explicitly specified and is mutable.

  • Check data type: type()
x = 1type(x)
>> int
  • Convert data type: int(), float(), str()

Primitive

1.Integers

2.Float (Decimal)

3.Strings

  • Concatenate using +. Works on numbers that are stored as string
x = ‘hello'
y = ‘world'
x + y
>> ‘hello world'
x = ’5'
y = ‘6'
x + y
>> ’56'
  • Repeat string using *
x = * 2
>> ‘hellohello'
  • Access string (positive and negative indexs)
x[1]
>> ‘e'
x[-3:-1]
>> ‘ll'
  • Slice string
x[1:]
>> ‘ello'
x[0] + x[4]
>> ‘ho'
  • Capitalize
x.capitalize()
>> ‘Hello'
str.capitalize(‘hello’)
>> ‘Hello'
  • Length
len(x)
>> 5
  • Replace string with other strings
x.replace(‘ello’, y)
>> ‘hworld'
x.replace(‘ello’, ‘ey’)
>> ‘hey'
  • Count occurrence of substring within string
a = ‘abcabcabcd'
b = ‘abc'
a.count(b)
>> 3
a.count(‘abcd’)
>> 1
  • Find lowest index within string where substring is found
x = ‘hello'
z = ‘ello'
x.find(z)
>> 1
x.find(‘llo’)
>> 2
  • Returns a list of words in a string using a delimiter
x = 'h e l l o'x.split(' ')
>> [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
  • Remove leading and trailing whitespace. lstrip() to remove leading whitespace and rstrip() to remove trailing whitespace
x = '    hello   'x.strip()
>> ‘hello'
  • Insert numbers into strings
x = ‘I have {} cherries and {} fishes.'
a = 5
b = 6
print(x.format(a, b))
>> I have 5 cherries and 6 fishes.

4.Boolean

  • Returns True or False (interchangeable with 1 and 0)

Non-Primitive

More sophisticated data structure which allows a collection of data in various formats

1.Array

  • Similar to lists, except that the types of objects store within it is constrained
  • Not the same as numpy array
  • Able to apply function to entire array as all objects are of the same data type. Faster processing and uses lesser memory
  • Type is specified at creation with type code eg ‘I’
import array as arrx = arr.array(’I’, [1,2,3])

2.List (create using [ ])

  • Store heterogeneous objects
  • Mutable
  • Access list using [ ]
x = [] # Create empty list
x = [1, 2, 3] # Create list
x = list([1, ‘apple’, 2, 3]) # x = [1, ‘apple’, 2, 3] is the samex[1]
>> ‘apple'
  • Replace objects in list
x[1] = ‘cherry’print(x)
>> [1, ‘cherry’, 2, 3]
  • Appending to list
x = [1,2,3,4,5]
x.append(6) #Adds to the last position by default
print(x)
>> [1,2,3,4,5,6]
  • Append to list at specific index
x.insert(0, 9)print(x)
>> [9,2,3,4,5]
  • Remove item at specified index
x.pop(0) #Remove object at index 0print(x)
>> [2,3,4,5]

3.NumPy Array

  • Mutable
  • Supports vectorized (element-wise addition, division etc) operations
import numpy as npx = np.array([2, 4, 6, 8])
y = x/2
print(y)
>> [1. 2. 3.]
  • Create vector of zeros, ones or a constant/string
print(np.ones(3))
>> [1. 1. 1.]
print(np.zeros((2,2)))
>> [[0. 0.]
[0. 0.]]
print(np.full((2,2), ‘x’) # Can also be an integer
>> [[‘x’ ‘x’]
[‘x’ ‘x’]]
  • Create identity matrix
print(np.eye(2,2))
>> [[1. 0.]
[0. 1.]]
  • Create vector of a sequence of numbers
np.arange(5)
>> array([0,1,2,3,4])
  • Access array and re-assignment
x = np.array([[2,4,6], [0,0,0]])x[0,1]
>> 6
x[0,1] = 7print(x)
>> [[ 2 4 7 ]
[ 0 0 0 ]]
y = np.array([1, 2, 3, 4, 5])y[::2] # x[start:stop:step]
>> array([1, 3, 5])
y[::-1] # Reverse objects within array
>> array([5, 4, 3, 2, 1])
  • Concatenate array
z = np.array([6, 7, 8, 9])np.concatenate([y, z])
>> array([1, 2, 3, 4, 5, 6, 7, 8, 9])
  • Split array
x = np.arange(10) x1, x2 = np.split(x, [3])
print(x1, x2)
>> [0 1 2] [3 4 5 6 7 8 9]
x1, x2, x3 = np.split(x, [3, 6])
print(x1, x2, x3)
>> [0 1 2] [3 4 5] [6 7 8 9]
  • Dimension of array
x = np.array([[2,4,6], [0,0,0]])x.shape
>> (2,3)
  • Data type/ type of data within array
type(x) # Data type
>> numpy.ndarray
x.dtype # Data type of object in array
>> dtype(‘int64’)
  • Convert from python list
py_list = [1, 2, 3, 4]np_arr = np.array(py_list)

4.Tuples (create using ( ))

  • Similar to lists except that they are immutable (cannot do assignment)
  • Since they are immutable, processing of tuples are faster as compared to lists
  • Access tuple using [ ]
x = 1,2,3,4
y = (‘a’, ‘b’, ‘c’)
x[0]
>> 1
x[0] = 2 # Tuples are immutable
>> ERROR!

5.Dictionary (create using { })

  • Key-Value pair structure. Keys must be unique within a dictionary
x = { } # Create empty dictionaryx = {‘apple’: 1, ‘orange’:3, ‘cherry’: 5, ‘fish’: 6}x[‘apple’]
>> 1
len(x)
>> 4
del x[‘apple’]x
>> {orange’:3, ‘cherry’: 5, ‘fish’: 6}

6.Sets

  • Collection of distinct objects (lists with no duplicates)
  • Unordered but mutable

--

--