lists



A list is an object that can hold other objects

  • they are represented with []

  • they can be made with the list constructor

  • they can be changed by performing an operation

requirements

  • I open a terminal to run makePythonTdd.sh with lists as the name of the project

    ./makePythonTdd.sh lists
    

    on Windows without Windows Subsystem Linux use makePythonTdd.ps1

    ./makePythonTdd.ps1 lists
    

    it makes the folders and files that are needed, installs packages, runs the first test, and the terminal shows AssertionError

    E       AssertionError: True is not false
    
    tests/test_lists.py:7: AssertionError
    
  • I hold ctrl (windows/linux) or option (mac) on the keyboard and use the mouse to click on tests/test_lists.py:7 to open it in the editor

  • then change True to False to make the test pass


test_make_a_list

red: make it fail

I change test_failure to test_make_a_list

import unittest


class TestLists(unittest.TestCase):

    def test_make_a_list(self):
        self.assertEqual(list(), None)

the terminal shows AssertionError

AssertionError: [] != None

green: make it pass

I change the expectation

self.assertEqual(list(), [])

the test passes, this is how to make an empty list

refactor: make it better

I add another assertion, this time with input to the list constructor

self.assertEqual(list(), [])
self.assertEqual(list(0), [])

the terminal shows TypeError

TypeError: 'int' object is not iterable

I add the error to the list of Exceptions encountered

# Exceptions Encountered
# AssertionError
# TypeError

I change the input to a tuple

self.assertEqual(list(), [])
self.assertEqual(list((0, 1, 2, 'n')), [])

the terminal shows AssertionError

AssertionError: Lists differ: [0, 1, 2, 'n'] != []

I change the expectation to match

self.assertEqual(list((0, 1, 2, 'n')), [0, 1, 2, 'n'])

the test passes. The tests show I can make a list with the list constructor and square brackets([]), which uses less characters than the constructor


test_attributes_and_methods_of_lists

I can use the dir function to see the attributes and methods of lists

red: make it fail

I add a failing test

def test_make_a_list(self):
    ...

def test_attributes_and_methods_of_lists(self):
  self.assertEqual(
      dir(list),
      [

      ]
  )

the terminal shows AssertionError

there is also a note on how to see the full difference between dir(list) and my empty list

Diff is 748 characters long. Set self.maxDiff to None to see it

unittest.TestCase.maxDiff is an attribute of the unittest.TestCase class that sets the maximum number of characters to show when comparing 2 objects in the terminal, when it is set to None it shows all the differences

green: make it pass

  • I move the terminal to the right then add self.maxDiff to the test

    def test_attributes_and_methods_of_lists(self):
        self.maxDiff = None
        self.assertEqual(
            dir(list),
            [
    
            ]
        )
    

    the terminal shows a long list of items. I copy and paste them from the terminal and use find and replace to remove the extra characters

    Note

    your results can be different because of your Python version

    def test_attributes_and_methods_of_lists(self):
        self.maxDiff = None
        self.assertEqual(
            dir(list),
            [
                '__add__',
                ...
                'append',
                'clear',
                'copy',
                'count',
                'extend',
                'index',
                'insert',
                'pop',
                'remove',
                'reverse',
                'sort'
            ]
        )
    

    the terminal shows passing tests and I move it back to the bottom. I ignore the names with double underscores (__) for now, then copy and paste the other names to make a TODO list for the next tests

    'append',
    'clear',
    'copy',
    'count',
    'extend',
    'index',
    'insert',
    'pop',
    'remove',
    'reverse',
    'sort'
    
    # Exceptions Encountered
    # AssertionError
    # TypeError
    

test_append_adds_to_a_list

red: make it fail

I add a test for the append method

def test_attributes_and_methods_of_lists(self):
    ...

def test_append(self):
    a_list = [0, 1, 2, 'n']
    self.assertIsNone(a_list.append())

the terminal shows TypeError

TypeError: list.append() takes exactly one argument (0 given)

green: make it pass

I add input

self.assertIsNone(a_list.append(0))

the terminal shows green, the append method returns None when it is called

refactor: make it better

  • I add another assertion to see what append did to the list

    self.assertIsNone(a_list.append(0))
    self.assertEqual(a_list, [0, 1, 2, 'n'])
    

    the terminal shows AssertionError

    AssertionError: Lists differ: [0, 1, 2, 'n', 0] != [0, 1, 2, 'n']
    

    the method added a value. I change the values in the test to match the values in the terminal

    self.assertEqual(a_list, [0, 1, 2, 'n', 0])
    

    the test passes

  • I change the value given to append for fun

    self.assertIsNone(a_list.append('n+1'))
    

    the terminal shows AssertionError

    AssertionError: Lists differ: [0, 1, 2, 'n', 'n+1'] != [0, 1, 2, 'n', 0]
    

    I change the expectation to match

    self.assertEqual(a_list, [0, 1, 2, 'n', 'n+1'])
    

    the test passes

  • I rename the test

    def test_append_adds_to_a_list(self):
        a_list = [0, 1, 2, 'n']
        self.assertIsNone(a_list.append('n+1'))
        self.assertEqual(a_list, [0, 1, 2, 'n', 'n+1'])
    
  • I remove append from the TODO list

    'clear',
    'copy',
    'count',
    'extend',
    'index',
    'insert',
    'pop',
    'remove',
    'reverse',
    'sort'
    

test_clear_empties_a_list

I add a test for the clear method

def test_append_adds_to_a_list(self):
    ...

def test_clear(self):
    a_list = [0, 1, 2, 'n']
    self.assertIsNone(a_list.clear())

the terminal shows green. The clear method returns None when it is called

red: make it fail

I add an assertion to show what it did to the list

self.assertIsNone(a_list.clear())
self.assertEqual(a_list, [0, 1, 2, 'n'])

the terminal shows AssertionError

AssertionError: Lists differ: [] != [0, 1, 2, 'n']

it is now empty

green: make it pass

I change the values to match

self.assertEqual(a_list, [])

the test passes

refactor: make it better

  • I change the name of the test to be more descriptive

    def test_clear_empties_a_list(self):
        a_list = [0, 1, 2, 'n']
        self.assertIsNone(a_list.clear())
        self.assertEqual(a_list, [])
    
  • I remove clear from the TODO list

    'copy',
    'count',
    'extend',
    'index',
    'insert',
    'pop',
    'remove',
    'reverse',
    'sort'
    

test_copy_a_list

red: make it fail

I add another test, I think we can guess what this one does

def test_clear_empties_a_list(self):
    ...

def test_copy(self):
    a_list = [0, 1, 2, 'n']
    self.assertIsNone(a_list.copy())

the terminal shows AssertionError

AssertionError: [0, 1, 2, 'n'] is not None

the method returns a copy of the list

green: make it pass

I add the value to the assertion

a_list = [0, 1, 2, 'n']
self.assertIsNone(a_list.copy(), [0, 1, 2, 'n'])

the terminal shows AssertionError

AssertionError: [0, 1, 2, 'n'] is not None : [0, 1, 2, 'n']

the method returns a copy of the list. I change assertIsNone to assertEqual

a_list = [0, 1, 2, 'n']
self.assertEqual(a_list.copy(), [0, 1, 2, 'n'])

the test passes

refactor: make it better

  • I add another assertion to see what happened to the original list

    self.assertEqual(a_list.copy(), [0, 1, 2, 'n'])
    self.assertEqual(a_list, [])
    

    the terminal shows AssertionError

    AssertionError: Lists differ: [0, 1, 2, 'n'] != []
    

    it stayed the same. I add the values

    self.assertEqual(a_list, [0, 1, 2, 'n'])
    

    the test passes

  • I change the name of the test

    def test_copy_a_list(self):
        a_list = [0, 1, 2, 'n']
        self.assertEqual(a_list.copy(), [0, 1, 2, 'n'])
        self.assertEqual(a_list, [0, 1, 2, 'n'])
    
  • I remove copy from the TODO list

    'count',
    'extend',
    'index',
    'insert',
    'pop',
    'remove',
    'reverse',
    'sort'
    

test_count_number_of_times_item_is_in_a_list

red: make it fail

I add a test for the next method

def test_count(self):
    a_list = [0, 1, 2, 'n']
    self.assertIsNone(a_list.count())

the terminal shows TypeError

TypeError: list.count() takes exactly one argument (0 given)

I pass a value to the call

def test_count(self):
    a_list = [0, 1, 2, 'n']
    self.assertIsNone(a_list.count(0))

the terminal shows AssertionError

AssertionError: 1 is not None

green: make it pass

I add the value

self.assertIsNone(a_list.count(0), 1)

the terminal shows AssertionError

AssertionError: 1 is not None : 1

I change the assertIsNone to assertEqual

a_list = [0, 1, 2, 'n']
self.assertEqual(a_list.count(0), 1)

the test passes

refactor: make it better

  • It looks like the count method returns the number of times an item is in a list. I change it then add another assertion to be sure

    def test_count(self):
        a_list = [0, 1, 2, 1, 'n', 1]
        self.assertEqual(a_list.count(0), 1)
        self.assertEqual(a_list.count(1), 1)
    

    the terminal shows AssertionError

    AssertionError: 3 != 1
    

    I change the value to match

    self.assertEqual(a_list.count(1), 3)
    

    the test passes

  • I add another assertion to see what happens when I try to count an item that is not in the list

    self.assertEqual(a_list.count(2), 3)
    self.assertEqual(a_list.count('not in list'), 3)
    

    the terminal shows AssertionError

    AssertionError: 0 != 3
    

    The count method returns 0 when the item is not in the list. I change the value to match

    self.assertEqual(a_list.count('not in list'), 0)
    

    the test passes

  • I rename the test

    def test_count_number_of_times_item_is_in_a_list(self):
        a_list = [0, 2, 1, 2, 3, 2]
        self.assertEqual(a_list.count(0), 1)
        self.assertEqual(a_list.count(2), 3)
        self.assertEqual(a_list.count(9), 0)
    
  • I remove count from the TODO list

    'extend',
    'index',
    'insert',
    'pop',
    'remove',
    'reverse',
    'sort'
    

test_extend_makes_a_list_longer

red: make it fail

time for another test

def test_count_number_of_times_item_is_in_a_list(self):
    ...

def test_extend(self):
    a_list = [0, 1, 2, 'n']
    self.assertIsNone(a_list.extend())

the terminal shows TypeError

TypeError: list.extend() takes exactly one argument (0 given)

green: make it pass

I pass a value to the call

self.assertIsNone(a_list.extend(0))

the terminal shows TypeError

TypeError: 'int' object is not iterable

I change the value to an iterable

self.assertIsNone(a_list.extend((0, 1)))

the test passes. The extend method returns None when called

refactor: make it better

  • I add another assertion to see what it did to the list

    self.assertIsNone(a_list.extend((0, 1)))
    self.assertEqual(a_list, [0, 1, 2, 'n'])
    

    the terminal shows AssertionError

    AssertionError: Lists differ: [0, 1, 2, 'n', 0, 1] != [0, 1, 2, 'n']
    

    I change the values in the test to match

    self.assertEqual(a_list, [0, 1, 2, 'n', 0, 1])
    

    the test passes

  • I change the values given to the extend method for fun

    def test_extend(self):
        a_list = [0, 1, 2, 'n']
        self.assertIsNone(a_list.extend((2, 1, 0)))
        self.assertEqual(a_list, [0, 1, 2, 'n', 0, 1])
    

    the terminal shows AssertionError

    AssertionError: Lists differ: [0, 1, 2, 'n', 2, 1, 0] != [0, 1, 2, 'n', 0, 1]
    

    I change the values to match

    self.assertEqual(a_list, [0, 1, 2, 'n', 2, 1, 0])
    

    the test is green again

  • I change the name of the test

    def test_extend_makes_a_list_longer(self):
        a_list = [0, 1, 2, 'n']
        self.assertIsNone(a_list.extend((2, 1, 0)))
        self.assertEqual(a_list, [0, 1, 2, 'n', 2, 1, 0])
    
  • I remove extend from the TODO list

    'index',
    'insert',
    'pop',
    'remove',
    'reverse',
    'sort'
    

test_index_returns_position_of_item_in_a_list

red: make it fail

I add a test for the index method

def test_extend_makes_a_list_longer(self):
    ...

def test_index(self):
    a_list = [0, 1, 2, 'n']
    self.assertIsNone(a_list.index())

the terminal shows TypeError

TypeError: index expected at least 1 argument, got 0

green: make it pass

I add a value to the call

def test_index(self):
    a_list = [0, 1, 2, 'n']
    self.assertIsNone(a_list.index(0))

the terminal shows AssertionError

AssertionError: 0 is not None

I add the expectation

AssertionError: 0 is not None : 0

I change the assertIsNone to assertEqual

def test_index(self):
    a_list = [0, 1, 2, 'n']
    self.assertEqual(a_list.index(0), 0)

the test passes

refactor: make it better

  • I change the values in a_list to see if the method is returning the same value I give it

    def test_index(self):
        a_list = ['1st', '2nd', '3rd', '...last']
        self.assertEqual(a_list.index(0), 0)
    

    the terminal shows ValueError

    ValueError: 0 is not in list
    

    the index method raises ValueError when the item is not in the list

  • I add it to the list of Exceptions encountered

    # Exceptions Encountered
    # AssertionError
    # TypeError
    # ValueError
    
  • I remove the things around the call and change the value to be more descriptive

    a_list.index('not in list')
    

    the terminal shows ValueError

    ValueError: 'not in list' is not in list
    
  • I add assertRaises to handle the Exception

    def test_index(self):
        a_list = ['1st', '2nd', '3rd', '...last']
    
        with self.assertRaises(ValueError):
            a_list.index('not in list')
    

    the test is green again

  • I add a new assertion for the index method

    def test_index(self):
        a_list = ['1st', '2nd', '3rd', '...last']
        self.assertEqual(a_list.index('1st'), '1st')
    
        with self.assertRaises(ValueError):
            a_list.index('not in list')
    

    the terminal shows AssertionError

    AssertionError: 0 != '1st'
    

    I change the expectation

    self.assertEqual(a_list.index('1st'), 0)
    

    the test passes

  • I add another assertion

    self.assertEqual(a_list.index('1st'), 0)
    self.assertEqual(a_list.index('3rd'), 0)
    

    the terminal shows AssertionError

    AssertionError: 2 != 0
    

    I change the value in the test

    self.assertEqual(a_list.index('3rd'), 2)
    

    the test passes

  • I add another assertion

    self.assertEqual(a_list.index('3rd'), 2)
    self.assertEqual(a_list.index('2nd'), 2)
    

    the terminal shows AssertionError

    AssertionError: 1 != 2
    

    I change the value to match

    self.assertEqual(a_list.index('2nd'), 1)
    

    the test is green again

  • I add another assertion

    self.assertEqual(a_list.index('2nd'), 1)
    self.assertEqual(a_list.index('...last'), 1)
    

    the terminal shows AssertionError

    AssertionError: 3 != 1
    

    I change the value to match the terminal

    self.assertEqual(a_list.index('...last'), 3)
    

    the test passes. The index method returns numbers for the position of the item in the the list. Python uses zero-based indexing which means the first item has an index of 0 and the last item has an index of the length of the list minus 1

  • I rename the test

    def test_index_returns_position_of_item_in_a_list(self):
        a_list = ['1st', '2nd', '3rd', '...last']
        self.assertEqual(a_list.index('1st'), 0)
        self.assertEqual(a_list.index('3rd'), 2)
        self.assertEqual(a_list.index('2nd'), 1)
        self.assertEqual(a_list.index('...last'), 3)
    
        with self.assertRaises(ValueError):
            a_list.index('not in list')
    
  • I also remove index from the TODO list

    'insert',
    'pop',
    'remove',
    'reverse',
    'sort'
    

test_insert_places_item_at_given_index_in_a_list

red: make it fail

I add a test for the next method

def test_index_returns_position_of_item_in_a_list(self):
    ...

def test_insert(self):
    a_list = [0, 1, 2, 'n']
    self.assertIsNone(a_list.insert())

the terminal shows TypeError

TypeError: insert expected 2 arguments, got 0

green: make it pass

I pass two values to the method

def test_insert(self):
    a_list = [0, 1, 2, 'n']
    self.assertIsNone(a_list.insert(0, 1))

the test is green. The insert method returns None

refactor: make it better

  • I add an assertion to find out what it did to the list

    self.assertIsNone(a_list.insert(0, 1))
    self.assertEqual(a_list, [0, 1, 2, 'n'])
    

    the terminal shows AssertionError

    AssertionError: Lists differ: [1, 0, 1, 2, 'n'] != [0, 1, 2, 'n']
    

    I add the new value to the list

    self.assertEqual(a_list, [1, 0, 1, 2, 'n'])
    

    the test passes. It looks like the insert method places the second input given at the index given as the first input

  • I change the second input in the call to be sure

    self.assertIsNone(a_list.insert(0, -1))
    

    the terminal shows AssertionError

    AssertionError: Lists differ: [-1, 0, 1, 2, 'n'] != [1, 0, 1, 2, 'n']
    

    I change the value to match

    self.assertEqual(a_list, [-1, 0, 1, 2, 'n'])
    

    the test is green again

  • I add another assertion with a call to the insert method

    self.assertEqual(a_list, [-1, 0, 1, 2, 'n'])
    self.assertIsNone(a_list.insert(3, 1.5))
    

    the terminal shows green. I add an assertion to see what it did to the list

    self.assertIsNone(a_list.insert(3, 1.5))
    self.assertEqual(a_list, [-1, 0, 1, 2, 'n'])
    

    the terminal shows AssertionError

    AssertionError: Lists differ: [-1, 0, 1, 1.5, 2, 'n'] != [-1, 0, 1, 2, 'n']
    

    I add the value to the expectation

    self.assertEqual(a_list, [-1, 0, 1, 1.5, 2, 'n'])
    

    the test passes

  • I rename the test

    def test_insert_places_item_at_given_index_in_a_list(self):
        a_list = [0, 1, 2, 'n']
        self.assertIsNone(a_list.insert(0, -1))
        self.assertEqual(a_list, [-1, 0, 1, 2, 'n'])
        self.assertIsNone(a_list.insert(3, 1.5))
        self.assertEqual(a_list, [-1, 0, 1, 1.5, 2, 'n'])
    
  • I remove insert from the TODO list

    'pop',
    'remove',
    'reverse',
    'sort'
    

test_pop_removes_and_returns_last_item_from_a_list

red: make it fail

I add a new test

def test_insert_places_item_at_given_index_in_a_list(self):
    ...

def test_pop(self):
    a_list = [0, 1, 2, 'n']
    self.assertIsNone(a_list.pop())

the terminal shows AssertionError

AssertionError: 'n' is not None

it looks like the pop method returns the last item in the list

green: make it pass

I add the expectation

self.assertIsNone(a_list.pop(), 'n')

the terminal shows AssertionError

AssertionError: 'n' is not None : n

I change the assertIsNone to assertEqual

a_list = [0, 1, 2, 'n']
self.assertEqual(a_list.pop(), 'n')

the test passes

refactor: make it better

  • I add an assertion to see what the method did to the list

    self.assertEqual(a_list.pop(), 'n')
    self.assertEqual(a_list, [0, 1, 2, 'n'])
    

    the terminal shows AssertionError

    AssertionError: Lists differ: [0, 1, 2] != [0, 1, 2, 'n']
    

    I change the values in the test to match

    self.assertEqual(a_list, [0, 1, 2])
    

    the test passes

  • I add another assertion

    self.assertEqual(a_list, [0, 1, 2])
    self.assertEqual(a_list.pop(), 'n')
    

    the terminal shows AssertionError

    AssertionError: 2 != 'n'
    

    I change the value in the test

    self.assertEqual(a_list, [0, 1, 2])
    self.assertEqual(a_list.pop(), 2)
    

    the test passes. The pop method removes and returns the last item in the list

  • I rename the test

    def test_pop_removes_and_returns_last_item_from_a_list(self):
        a_list = [0, 1, 2, 'n']
        self.assertEqual(a_list.pop(), 'n')
        self.assertEqual(a_list, [0, 1, 2])
        self.assertEqual(a_list.pop(), 2)
    
  • I remove pop from the TODO list

    'remove',
    'reverse',
    'sort'
    

test_remove_first_instance_of_item_from_a_list

red: make it fail

time for the next method

def test_remove(self):
    a_list = [0, 1, 2, 'n']
    self.assertIsNone(a_list.remove())

the terminal shows TypeError

TypeError: list.remove() takes exactly one argument (0 given)

green: make it pass

I add a value to the call

self.assertIsNone(a_list.remove(0))

the terminal shows green, the remove method returns None

refactor: make it better

  • I add an assertion to see what remove did to the list

    self.assertIsNone(a_list.remove(0))
    self.assertEqual(a_list, [0, 1, 2, 'n'])
    

    the terminal shows AssertionError

    AssertionError: Lists differ: [1, 2, 'n'] != [0, 1, 2, 'n']
    

    the method removes the item given from the list. I change the expectation

    self.assertEqual(a_list, [1, 2, 'n'])
    

    the test passes

  • I change the values in a_list to see what would happen if an item shows up more than once in the list

    def test_remove(self):
        a_list = [0, 1, 0, 2, 0, 'n']
        self.assertIsNone(a_list.remove(0))
        self.assertEqual(a_list, [1, 2, 'n'])
    

    the terminal shows AssertionError

    AssertionError: Lists differ: [1, 0, 2, 0, 'n'] != [1, 2, 'n']
    

    the method removes the first instance of the given item from the list. I change the values to match

    self.assertEqual(a_list, [1, 0, 2, 0, 'n'])
    

    the test passes

  • I want to know what happens when the item is not in the list

    self.assertEqual(a_list, [1, 0, 2, 0, 'n'])
    self.assertIsNone(a_list.remove('not in list'))
    

    the terminal shows ValueError

    ValueError: list.remove(x): x not in list
    

    I remove the things around the call then add assertRaises

    with self.assertRaises(ValueError):
        a_list.remove('not in list')
    

    the test passes

  • I rename the test

    def test_remove_first_instance_of_item_from_a_list(self):
        a_list = [0, 1, 0, 2, 0, 'n']
        self.assertIsNone(a_list.remove(0))
        self.assertEqual(a_list, [1, 0, 2, 0, 'n'])
    
        with self.assertRaises(ValueError):
            a_list.remove('not in list')
    
  • I take out remove from the TODO list

    'reverse',
    'sort'
    

test_reverse_a_list

red: make it fail

I add the next test

def test_reverse(self):
    a_list = [0, 1, 2, 'n']
    self.assertIsNone(a_list.reverse())

the terminal shows green. This method returns None. I add an assertion to see what it did to the list

self.assertIsNone(a_list.reverse())
self.assertEqual(a_list, [0, 1, 2, 'n'])

the terminal shows AssertionError

AssertionError: Lists differ: ['n', 2, 1, 0] != [0, 1, 2, 'n']

the method reverses the order of the list

green: make it pass

I change the expectation

self.assertEqual(a_list, ['n', 2, 1, 0])

the test passes

refactor: make it better

  • I rename the test

    def test_reverse_a_list(self):
        a_list = [0, 1, 2, 'n']
        self.assertIsNone(a_list.reverse())
        self.assertEqual(a_list, ['n', 2, 1, 0])
    
  • I remove the name from the TODO list

    'sort'
    

test_sort_a_list

red: make it fail

I add a test

def test_sort(self):
    a_list = [0, 1, 2, 'n']
    self.assertIsNone(a_list.sort())

the terminal shows TypeError

TypeError: '<' not supported between instances of 'str' and 'int'

I have to change 'n' to a number or change the other numbers to a string

green: make it pass

I remove the things around the call then add assertRaises

def test_sort(self):
    with self.assertRaises(TypeError):
        [0, 1, 2, 'n'].sort()

the test passes

refactor: make it better

  • I add another assertion

    def test_sort(self):
        with self.assertRaises(TypeError):
            [0, 1, 2, 'n'].sort()
    
        a_list = [0, 1, 2, 3]
        self.assertIsNone(a_list.sort())
    

    the terminal still shows green, sort returns None when called. I add another assertion to see what it did to the list

    self.assertIsNone(a_list.sort())
    self.assertEqual(a_list, [])
    

    the terminal shows AssertionError

    AssertionError: Lists differ: [0, 1, 2, 3] != []
    

    the list is still the same. I change the expectation

    self.assertEqual(a_list, [0, 1, 2, 3])
    

    the test passes. The name of the method is sort and the list is already sorted. I change it to see what would happen when it is not sorted

    a_list = [0, 1, -1, 2, -2, 3, -3]
    

    I get AssertionError

    AssertionError: Lists differ: [-3, -2, -1, 0, 1, 2, 3] != [0, 1, 2, 3]
    

    the sort method arranges the list in ascending order. I change the values to match

    self.assertEqual(a_list, [-3, -2, -1, 0, 1, 2, 3])
    

    the test passes

  • I change the name of the test

    def test_sort_a_list(self):
        with self.assertRaises(TypeError):
            [0, 1, 2, 'n'].sort()
    
        a_list = [0, 1, -1, 2, -2, 3, -3]
        self.assertIsNone(a_list.sort())
        self.assertEqual(a_list, [-3, -2, -1, 0, 1, 2, 3])
    
  • I remove sort from the TODO list


test_get_items_from_a_list

I can provide the index of an item I want to see in square brackets([]) to a list

red: make it fail

I add a failing test

def test_get_items_from_a_list(self):
    a_list = ['1st', '2nd', '3rd', '...last']
    self.assertEqual(a_list[0], '')

the terminal shows AssertionError

AssertionError: '1st' != ''

the first item has an index of 0 as shown in test_index_returns_position_of_item_in_a_list

green: make it pass

I change the value in the test

self.assertEqual(a_list[0], '1st')

the test passes

refactor: make it better

  • I can also use negative numbers. The last item has an index of -1 and the first item has an index of negative the length of the list

    def test_get_items_from_a_list(self):
        a_list = ['1st', '2nd', '3rd', '...last']
        self.assertEqual(a_list[0], '1st')
        self.assertEqual(a_list[-4], '')
    

    the terminal shows AssertionError

    AssertionError: '1st' != ''
    

    I change the value to match

    self.assertEqual(a_list[-4], '1st')
    

    the test passes

  • I add another assertion

    self.assertEqual(a_list[-4], '1st')
    self.assertEqual(a_list[2], '')
    

    the terminal shows AssertionError

    AssertionError: '3rd' != ''
    

    I change the expectation to match

    self.assertEqual(a_list[2], '3rd')
    

    the terminal shows green again

  • I add another assertion

    self.assertEqual(a_list[2], '3rd')
    self.assertEqual(a_list[-2], '')
    

    and get AssertionError

    AssertionError: '3rd' != ''
    

    I change the expectation

    self.assertEqual(a_list[-2], '3rd')
    

    the test passes

  • I add another line

    self.assertEqual(a_list[-2], '3rd')
    self.assertEqual(a_list[1], '')
    

    the terminal shows AssertionError

    AssertionError: '2nd' != ''
    

    I make the test pass

    self.assertEqual(a_list[1], '2nd')
    
  • I add another one

    self.assertEqual(a_list[1], '2nd')
    self.assertEqual(a_list[-3], '')
    

    the terminal shows AssertionError

    AssertionError: '2nd' != ''
    

    I change the value

    self.assertEqual(a_list[-3], '2nd')
    

    the test passes

  • I add another assertion

    self.assertEqual(a_list[-3], '2nd')
    self.assertEqual(a_list[3], '')
    

    and get AssertionError

    AssertionError: '...last' != ''
    

    I change the value

    self.assertEqual(a_list[3], '...last')
    

    the test passes

  • I add another assertion

    self.assertEqual(a_list[3], '...last')
    self.assertEqual(a_list[-1], '')
    

    the terminal shows AssertionError

    AssertionError: '...last' != ''
    

    I make the values match

    self.assertEqual(a_list[-1], '...last')
    

    the test passes. This is also called indexing


test_set_items_in_a_list

red: make it fail

I add another test

def test_set_items_in_a_list(self):
    a_list = ['1st', '2nd', '3rd', '...last']
    a_list[-1] = '4th'
    self.assertEqual(a_list, ['1st', '2nd', '3rd', '...last'])

the terminal shows AssertionError

AssertionError: Lists differ: ['1st', '2nd', '3rd', '4th'] != ['1st', '2nd', '3rd', '...last']

I can use the index of an item to change its value in a list

green: make it pass

I change the expectation to match the terminal

self.assertEqual(a_list, ['1st', '2nd', '3rd', '4th'])

the test passes


test_view_parts_of_a_list_aka_slicing

red: make it fail

I add another test

def test_set_items_in_a_list(self):
    ...

def test_view_parts_of_a_list(self):
    a_list = ['a', 'b', 'c', 'd']
    self.assertEqual(a_list[0:2], [])

the terminal shows AssertionError

AssertionError: Lists differ: ['a', 'b'] != []

viewing parts of a list is like indexing, it takes two values in square brackets([]), separated by a :, the first value is the starting index you want and the second value is the ending index plus 1

green: make it pass

I change the values to match

self.assertEqual(a_list[0:2], ['a', 'b'])

the test passes

refactor: make it better

  • I add another assertion

    self.assertEqual(a_list[0:2], ['a', 'b'])
    self.assertEqual(a_list[1:4], [])
    

    the terminal shows AssertionError

    AssertionError: Lists differ: ['b', 'c', 'd'] != []
    

    I add the missing values

    self.assertEqual(a_list[1:4], ['b', 'c', 'd'])
    

    the test passes

  • I add another line

    self.assertEqual(a_list[1:4], ['b', 'c', 'd'])
    self.assertEqual(a_list[0:3], [])
    

    the terminal shows AssertionError

    AssertionError: Lists differ: ['a', 'b', 'c'] != []
    

    I change the expectation

    self.assertEqual(a_list[0:3], ['a', 'b', 'c'])
    

    the test is green again

  • One more assert method for good measure

    self.assertEqual(a_list[0:3], ['a', 'b', 'c'])
    self.assertEqual(a_list[1:3], [])
    

    I get AssertionError

    AssertionError: Lists differ: ['b', 'c'] != []
    

    I change the expectation

    self.assertEqual(a_list[1:3], ['b', 'c'])
    

    the test is green again

  • This is also called slicing, I change the name of the test

    def test_view_parts_of_a_list_aka_slicing(self):
        a_list = ['a', 'b', 'c', 'd']
        self.assertEqual(a_list[0:2], ['a', 'b'])
        self.assertEqual(a_list[1:4], ['b', 'c', 'd'])
        self.assertEqual(a_list[0:3], ['a', 'b', 'c'])
        self.assertEqual(a_list[1:3], ['b', 'c'])
    

test_index_error

ValueError was raised earlier in test_remove_first_instance_of_item_from_a_list and test_index_returns_position_of_item_in_a_list, there is another Exception that is important to know, the IndexError is raised when I try to get an item from a list but use a number that is NOT in the range of indexes for it

red: make it fail

I add a failing test to show this

def test_index_error(self):
    a_list = ['a', 'b', 'c', 'd']
    a_list[4]

the terminal shows IndexError

IndexError: list index out of range

green: make it pass

  • I add IndexError to the list of Exceptions encountered

    # Exceptions Encountered
    # AssertionError
    # TypeError
    # ValueError
    # IndexError
    
  • I add assertRaises to handle the error

    def test_index_error(self):
        a_list = ['a', 'b', 'c', 'd']
    
        with self.assertRaises(IndexError):
            a_list[4]
    

    the test passes

refactor: make it better

  • I add another assertion to test indexing with a negative number that is outside the range of numbers that can be used to index the list

    with self.assertRaises(IndexError):
        a_list[4]
    a_list[-5]
    

    the terminal shows IndexError

    IndexError: list index out of range
    

    I add another assertRaises

    with self.assertRaises(IndexError):
        a_list[4]
    with self.assertRaises(IndexError):
        a_list[-5]
    

    the test passes

  • IndexError is also raised with the pop method when the the list is empty

    with self.assertRaises(IndexError):
        a_list[-5]
    [].pop()
    

    the terminal shows IndexError

    IndexError: pop from empty list
    

    I add assertRaises

    with self.assertRaises(IndexError):
        a_list[-5]
    with self.assertRaises(IndexError):
        [].pop()
    

    the terminal shows green


review

I ran tests for lists

  • they are represented with []

  • they can be made with the list constructor

  • they can hold any object

  • they can be changed by performing an operation

Would you like to test list comprehensions?


data structures: Lists: tests