what is a list?


A list is a container object that can hold any objects

  • they are represented with []

  • they can be made with list() or []

  • they can be changed with an operation, which means they are mutable

preview

I have these tests by the end of the chapter

lists/tests/test_lists.py
1import unittest
2
3
4class TestLists(unittest.TestCase):
5
6    def test_making_a_list(self):
7        self.assertEqual(list(), [])
8        self.assertEqual(list((0, 1, 2, 'n')), [0, 1, 2, 'n'])
lists/tests/test_lists.py
10    def test_dir_list(self):
11        self.maxDiff = None
12
13        dir_list = dir(object)
14        dir_list.extend((
15            '__add__', '__class_getitem__',
16            '__contains__', '__delitem__',
17            '__getitem__', '__iadd__',
18            '__imul__', '__iter__', '__len__',
19            '__mul__', '__reversed__',
20            '__rmul__', '__setitem__',
21            'append', 'clear', 'copy',
22            'count', 'extend', 'index',
23            'insert', 'pop', 'remove',
24            'reverse', 'sort',
25        ))
26        dir_list.sort()
27        self.assertEqual(dir(list), dir_list)
lists/tests/test_lists.py
29    def test_append_adds_item_to_end_of_a_list(self):
30        a_list = [0, 1, 2, 'n']
31        self.assertIsNone(a_list.append('n+1'))
32        self.assertEqual(a_list, [0, 1, 2, 'n', 'n+1'])
33
34    def test_clear_empties_a_list(self):
35        a_list = [0, 1, 2, 'n']
36        self.assertIsNone(a_list.clear())
37        self.assertEqual(a_list, [])
lists/tests/test_lists.py
39    def test_copy_a_list(self):
40        a_list = [0, 1, 2, 'n']
41        self.assertEqual(a_list.copy(), [0, 1, 2, 'n'])
42
43    def test_count_number_of_times_item_is_in_a_list(self):
44        a_list = [0, 1, 2, 1, 'n', 1]
45        self.assertEqual(a_list.count(0), 1)
46        self.assertEqual(a_list.count(1), 3)
47        self.assertEqual(a_list.count('not in list'), 0)
lists/tests/test_lists.py
49    def test_extend_adds_items_from_an_iterable_to_end_of_a_list(self):
50        a_list = [0, 1, 2, 'n']
51        self.assertIsNone(a_list.extend((2, 1, 0)))
52        self.assertEqual(a_list, [0, 1, 2, 'n', 2, 1, 0])
53
54        another_list = [0, 1, 2, 'n']
55        for number in (2, 1, 0):
56            another_list.append(number)
57        self.assertEqual(another_list, a_list)
lists/tests/test_lists.py
59    def test_index_returns_the_first_position_of_a_given_item(self):
60        a_list = ['1st', '2nd', '3rd', '...last', '1st']
61        self.assertEqual(a_list.index('1st', 0), 0)
62        self.assertEqual(a_list.index('3rd', 0), 2)
63        self.assertEqual(a_list.index('2nd', 0), 1)
64        self.assertEqual(a_list.index('...last', 0), 3)
65        self.assertEqual(a_list.index('1st', 1), 4)
66
67        with self.assertRaises(ValueError):
68            a_list.index('not in list')
lists/tests/test_lists.py
70    def test_insert_item_at_position_in_a_list(self):
71        a_list = [0, 1, 2, 'n']
72        self.assertIsNone(a_list.insert(0, -1))
73        self.assertEqual(a_list, [-1, 0, 1, 2, 'n'])
74        self.assertIsNone(a_list.insert(3, 1.5))
75        self.assertEqual(a_list, [-1, 0, 1, 1.5, 2, 'n'])
76
77    def test_pop_removes_and_returns_last_item_from_a_list(self):
78        a_list = [0, 1, 2, 'n']
79        self.assertEqual(a_list.pop(), 'n')
80        self.assertEqual(a_list, [0, 1, 2])
81        self.assertEqual(a_list.pop(), 2)
82        self.assertEqual(a_list, [0, 1])
lists/tests/test_lists.py
84    def test_remove_first_time_item_is_in_a_list(self):
85        a_list = [0, 1, 0, 2, 0, 'n']
86        self.assertIsNone(a_list.remove(0))
87        self.assertEqual(a_list, [1, 0, 2, 0, 'n'])
88
89        with self.assertRaises(ValueError):
90            a_list.remove('not in list')
91
92    def test_reverse_a_list(self):
93        a_list = [0, 1, 2, 'n']
94        self.assertIsNone(a_list.reverse())
95        self.assertEqual(a_list, ['n', 2, 1, 0])
lists/tests/test_lists.py
 97    def test_sort_a_list(self):
 98        with self.assertRaises(TypeError):
 99            [0, 1, 2, 'n'].sort()
100
101        a_list = [0, 1, -1, 2, -2, 3, -3]
102        self.assertIsNone(a_list.sort())
103        self.assertEqual(a_list, [-3, -2, -1, 0, 1, 2, 3])
lists/tests/test_lists.py
105    def test_getting_items_of_a_list(self):
106        a_list = ['1st', '2nd', '3rd', '...last']
107        self.assertEqual(a_list[0], '1st')
108        self.assertEqual(a_list[a_list.index('1st')], '1st')
109        self.assertEqual(a_list[-4], '1st')
110        self.assertEqual(a_list[2], '3rd')
111        self.assertEqual(a_list[-2], '3rd')
112        self.assertEqual(a_list[1], '2nd')
113        self.assertEqual(a_list[-3], '2nd')
114        self.assertEqual(a_list[3], '...last')
115        self.assertEqual(a_list[-1], '...last')
lists/tests/test_lists.py
117    def test_setting_items_in_a_list(self):
118        a_list = ['1st', '2nd', '3rd', '...last']
119        a_list[-1] = '4th'
120        self.assertEqual(
121            a_list, ['1st', '2nd', '3rd', '4th']
122        )
123
124    def test_getting_parts_of_a_list_aka_slicing(self):
125        a_list = ['a', 'b', 'c', 'd']
126        self.assertEqual(a_list[0:2], ['a', 'b'])
127        self.assertEqual(a_list[:2], ['a', 'b'])
128        self.assertEqual(a_list[1:4], ['b', 'c', 'd'])
129        self.assertEqual(a_list[1:], ['b', 'c', 'd'])
130        self.assertEqual(a_list[0:3], ['a', 'b', 'c'])
131        self.assertEqual(a_list[1:3], ['b', 'c'])
132        self.assertEqual(a_list[:], a_list.copy())
lists/tests/test_lists.py
134    def test_index_error(self):
135        a_list = ['a', 'b', 'c', 'd']
136
137        with self.assertRaises(IndexError):
138            a_list[4]
139        with self.assertRaises(IndexError):
140            a_list[-5]
141        with self.assertRaises(IndexError):
142            [].pop()
143        with self.assertRaises(IndexError):
144            [][-1]
145
146
147# Exceptions seen
148# AssertionError
149# TypeError
150# ValueError
151# IndexError

questions about lists


start the project

  • I open a terminal

  • I give lists as the first argument to the makePythonTdd program

    terminal
    ./makePythonTdd.sh lists
    
    terminal
    .\makePythonTdd.ps1 lists
    

    the terminal is my friend, and shows AssertionError

    terminal
    ======================== FAILURES ==========================
    ______________ Testlists.test_failure _________________
    
    self = <tests.test_lists.Testlists testMethod=test_failure>
    
        def test_failure(self):
    >       self.assertFalse(True)
    E       AssertionError: True is not false
    
    tests/test_lists.py:7: AssertionError
    ================== short test summary info ==================
    FAILED tests/test_lists.py::Testlists::test_failure - AssertionError: True is not false
    ===================== 1 failed in X.YZs =====================
    
  • I open test_lists.py from the tests folder

  • I change True to False

    lists/tests/test_lists.py
     4class Testlists(unittest.TestCase):
     5
     6    def test_failure(self):
     7        self.assertFalse(False)
     8
     9
    10# Exceptions seen
    

    the test passes.

  • I change Testlists to TestLists to match Python convention

    lists/tests/test_lists.py
    1import unittest
    2
    3
    4class TestLists(unittest.TestCase):
    5
    6    def test_failure(self):
    
  • I open a new terminal then change directory to lists

    terminal
    cd lists
    
  • I add the new files and folders to git for tracking

    terminal
    git add .
    
  • I add a git commit message

    terminal
    git commit -am 'setup project'
    

test_making_a_list

RED: make it fail


I change test_failure to test_making_a_list

lists/tests/test_lists.py
4class TestLists(unittest.TestCase):
5
6    def test_making_a_list(self):
7        self.assertEqual(list(), None)

the terminal is my friend, and shows AssertionError

terminal
AssertionError: [] != None

GREEN: make it pass


I change the expectation

lists/tests/test_lists.py
7        self.assertEqual(list(), [])

the test passes. I can make an empty list with list() or []


REFACTOR: make it better


  • I add another assertion with input to list() to make a list that has things in it

    lists/tests/test_lists.py
     6    def test_making_a_list(self):
     7        self.assertEqual(list(), [])
     8        self.assertEqual(list(0), [])
     9
    10
    11# Exceptions seen
    

    the terminal is my friend, and shows TypeError

    terminal
    TypeError: 'int' object is not iterable
    
  • I add TypeError to the list of Exceptions seen

    lists/tests/test_lists.py
    11# Exceptions seen
    12# AssertionError
    13# TypeError
    

what is an iterable?

An iterable is an object that can return what it has, one at a time. In order words, I can loop over it. strings, tuples, lists, sets and dictionaries are iterable.

  • I change the input to a tuple

    lists/tests/test_lists.py
     6    def test_making_a_list(self):
     7        self.assertEqual(list(), [])
     8        self.assertEqual(list((0, 1, 2, 'n')), [])
     9
    10
    11# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: Lists differ: [0, 1, 2, 'n'] != []
    
  • I change the expectation to match

    lists/tests/test_lists.py
    8        self.assertEqual(list((0, 1, 2, 'n')), [0, 1, 2, 'n'])
    

    the test passes.

  • I add a git commit message

    terminal
    git commit -am 'add test_making_a_list'
    

I can make a list with ‘list()’, or with square brackets(‘[]’) which uses less characters.


test_dir_list

I want to test the things I can do with lists. I use the dir built-in function to see the attributes and methods of lists


RED: make it fail


I add a failing test for the attributes and methods of lists

lists/tests/test_lists.py
 8        self.assertEqual(list((0, 1, 2, 'n')), [0, 1, 2, 'n'])
 9
10    def test_dir_list(self):
11        self.assertEqual(
12            dir(list),
13            []
14        )
15
16
17# Exceptions seen

the terminal is my friend, and shows AssertionError

terminal
AssertionError: Lists differ: [
    '__add__', '__class__', '__class_getitem_[552 chars]ort'
] != []

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

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 the full difference.


GREEN: make it pass


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

    lists/tests/test_lists.py
    10    def test_dir_list(self):
    11        self.maxDiff = None
    12        self.assertEqual(
    13            dir(list),
    14            []
    15        )
    

    the terminal is my friend, and shows a long list of items. I copy (ctrl/command+c) and paste (ctrl/command+v) them then use find and replace to remove the extra characters E and -

    Note

    results can be different because of the Python version

    lists/tests/test_lists.py
    10    def test_dir_list(self):
    11            self.maxDiff = None
    12            self.assertEqual(
    13                dir(list),
    14                [
    15                    '__add__',
    16                    '__class__',
    17                    '__class_getitem__',
    18                    '__contains__',
    19                    '__delattr__',
    20                    '__delitem__',
    21                    '__dir__',
    22                    '__doc__',
    23                    '__eq__',
    24                    '__format__',
    25                    '__ge__',
    26                    '__getattribute__',
    27                    '__getitem__',
    28                    '__getstate__',
    29                    '__gt__',
    30                    '__hash__',
    31                    '__iadd__',
    32                    '__imul__',
    33                    '__init__',
    34                    '__init_subclass__',
    35                    '__iter__',
    36                    '__le__',
    37                    '__len__',
    38                    '__lt__',
    39                    '__mul__',
    40                    '__ne__',
    41                    '__new__',
    42                    '__reduce__',
    43                    '__reduce_ex__',
    44                    '__repr__',
    45                    '__reversed__',
    46                    '__rmul__',
    47                    '__setattr__',
    48                    '__setitem__',
    49                    '__sizeof__',
    50                    '__str__',
    51                    '__subclasshook__',
    52                    'append',
    53                    'clear',
    54                    'copy',
    55                    'count',
    56                    'extend',
    57                    'index',
    58                    'insert',
    59                    'pop',
    60                    'remove',
    61                    'reverse',
    62                    'sort'
    63                ]
    64            )
    

    the test passes. This looks like the attributes and methods of objects with more stuff.

  • I copy (ctrl/command+c) and paste (ctrl/command+v) the names that do NOT have double underscores (__) to use as a TODO list

    lists/tests/test_lists.py
    67'append',
    68'clear',
    69'copy',
    70'count',
    71'extend',
    72'index',
    73'insert',
    74'pop',
    75'remove',
    76'reverse',
    77'sort'
    78
    79
    80# Exceptions seen
    81# AssertionError
    82# TypeError
    
  • I add a git commit message

    terminal
    git commit -am 'add test_dir_list'
    

The dir built-in function returns a list of attributes and methods.


test_append_adds_item_to_end_of_a_list

RED: make it fail


I add a test for the append method

lists/tests/test_lists.py
62                  'sort'
63              ]
64          )
65
66      def test_append(self):
67          a_list = [0, 1, 2, 'n']
68          self.assertIsNone(a_list.append())
69
70
71  'append',

the terminal is my friend, and shows TypeError

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

GREEN: make it pass


I add 0 as input

lists/tests/test_lists.py
68        self.assertIsNone(a_list.append(0))

the test is green again, the append method returns None when called.


REFACTOR: make it better


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

    lists/tests/test_lists.py
    66    def test_append(self):
    67        a_list = [0, 1, 2, 'n']
    68        self.assertIsNone(a_list.append(0))
    69        self.assertEqual(a_list, [0, 1, 2, 'n'])
    70
    71
    72'append',
    

    the terminal is my friend, and shows AssertionError

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

    the append method added a value at the end of the list.

  • I change the expectation to match the values in the terminal

    lists/tests/test_lists.py
    69        self.assertEqual(a_list, [0, 1, 2, 'n', 0])
    

    the test passes.

  • I change the value given to append

    lists/tests/test_lists.py
    66    def test_append(self):
    67        a_list = [0, 1, 2, 'n']
    68        self.assertIsNone(a_list.append('n+1'))
    69        self.assertEqual(a_list, [0, 1, 2, 'n', 0])
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: Lists differ: [0, 1, 2, 'n', 'n+1']
                               != [0, 1, 2, 'n', 0]
    
  • I change the expectation of the assertion to match

    lists/tests/test_lists.py
    66    def test_append(self):
    67        a_list = [0, 1, 2, 'n']
    68        self.assertIsNone(a_list.append('n+1'))
    69        self.assertEqual(a_list, [0, 1, 2, 'n', 'n+1'])
    70
    71
    72'append',
    

    the test passes.

  • I change the name of the test

    lists/tests/test_lists.py
    60                'remove',
    61                'reverse',
    62                'sort'
    63            ]
    64        )
    65
    66    def test_append_adds_item_to_end_of_a_list(self):
    67        a_list = [0, 1, 2, 'n']
    68        self.assertIsNone(a_list.append('n+1'))
    69        self.assertEqual(a_list, [0, 1, 2, 'n', 'n+1'])
    
  • I remove append from the TODO list

    lists/tests/test_lists.py
    69      self.assertEqual(a_list, [0, 1, 2, 'n', 'n+1'])
    70
    71
    72'clear',
    73'copy',
    74'count',
    75'extend',
    76'index',
    77'insert',
    78'pop',
    79'remove',
    80'reverse',
    81'sort'
    
  • I add a git commit message

    terminal
    git commit -am \
    'add test_append_adds_item_to_end_of_a_list'
    

I know how to add things to the end of a list


test_clear_empties_a_list

I add a test for the clear method

lists/tests/test_lists.py
69        self.assertEqual(a_list, [0, 1, 2, 'n', 'n+1'])
70
71    def test_clear(self):
72        a_list = [0, 1, 2, 'n']
73        self.assertIsNone(a_list.clear())
74
75
76'clear',

the test is still green. The clear method returns None when called


RED: make it fail


I add an assertion to see what clear did to the list

lists/tests/test_lists.py
71    def test_clear(self):
72        a_list = [0, 1, 2, 'n']
73        self.assertIsNone(a_list.clear())
74        self.assertEqual(a_list, [0, 1, 2, 'n'])
75
76
77'clear',

the terminal is my friend, and shows AssertionError

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

the list is now empty.


GREEN: make it pass


I change the values to match

lists/tests/test_lists.py
74        self.assertEqual(a_list, [])

the test passes.


REFACTOR: make it better


  • I change the name of the test to make it “clearer”

    lists/tests/test_lists.py
    69        self.assertEqual(a_list, [0, 1, 2, 'n', 'n+1'])
    70
    71    def test_clear_empties_a_list(self):
    72        a_list = [0, 1, 2, 'n']
    73        self.assertIsNone(a_list.clear())
    74        self.assertEqual(a_list, [])
    
  • I remove clear from the TODO list

    lists/tests/test_lists.py
    74        self.assertEqual(a_list, [])
    75
    76
    77'copy',
    78'count',
    79'extend',
    80'index',
    81'insert',
    82'pop',
    83'remove',
    84'reverse',
    85'sort'
    
  • I add a git commit message

    terminal
    git commit -am 'add test_clear_empties_a_list'
    

I know how to empty a list


test_copy_a_list

RED: make it fail


I add a test for the copy method

lists/tests/test_lists.py
74        self.assertEqual(a_list, [])
75
76    def test_copy(self):
77        a_list = [0, 1, 2, 'n']
78        self.assertIsNone(a_list.copy())
79
80
81'copy',

the terminal is my friend, and shows AssertionError

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

the method returns a copy of the list.


GREEN: make it pass


  • I add the list to the assertion

    lists/tests/test_lists.py
    77        a_list = [0, 1, 2, 'n']
    78        self.assertIsNone(a_list.copy(), [0, 1, 2, 'n'])
    

    the terminal is my friend, and shows AssertionError

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

    the values are the same, the problem is assertIsNone only takes 1 input and I gave it 2

  • I change assertIsNone to the assertEqual method

    lists/tests/test_lists.py
    77        a_list = [0, 1, 2, 'n']
    78        self.assertEqual(a_list.copy(), [0, 1, 2, 'n'])
    

    the test passes.


REFACTOR: make it better


  • I change the name of the test

    lists/tests/test_lists.py
    74        self.assertEqual(a_list, [])
    75
    76    def test_copy_a_list(self):
    77        a_list = [0, 1, 2, 'n']
    78        self.assertEqual(a_list.copy(), [0, 1, 2, 'n'])
    79
    80
    81'copy',
    
  • I remove copy from the TODO list

    lists/tests/test_lists.py
    78        self.assertIsNone(a_list.copy(), [0, 1, 2, 'n'])
    79
    80
    81'count',
    82'extend',
    83'index',
    84'insert',
    85'pop',
    86'remove',
    87'reverse',
    88'sort'
    
  • I add a git commit message

    terminal
    git commit -am 'add test_copy_a_list'
    

I know how to copy a list


test_count_number_of_times_item_is_in_a_list

RED: make it fail


I add a test for the count method

lists/tests/test_lists.py
78        self.assertEqual(a_list.copy(), [0, 1, 2, 'n'])
79
80    def test_count(self):
81        a_list = [0, 1, 2, 'n']
82        self.assertIsNone(a_list.count())
83
84
85'count',

the terminal is my friend, and shows TypeError

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

I add a value to the call

lists/tests/test_lists.py
80    def test_count(self):
81        a_list = [0, 1, 2, 'n']
82        self.assertIsNone(a_list.count(0))

the terminal is my friend, and shows AssertionError

terminal
AssertionError: 1 is not None

GREEN: make it pass


I add the value

lists/tests/test_lists.py
82        self.assertIsNone(a_list.count(0), 1)

the terminal is my friend, and shows AssertionError

terminal
AssertionError: 1 is not None : 1

I change assertIsNone to the assertEqual method

lists/tests/test_lists.py
82        self.assertEqual(a_list.count(0), 1)

the test passes.


REFACTOR: make it better


  • 0 is in this list one time, I add 1 to the list two more times then add an assertion for it

    lists/tests/test_lists.py
    80    def test_count(self):
    81        a_list = [0, 1, 2, 1, 'n', 1]
    82        self.assertEqual(a_list.count(0), 1)
    83        self.assertEqual(a_list.count(1), 1)
    84
    85
    86'count',
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: 3 != 1
    
  • I change the expectation to match

    lists/tests/test_lists.py
    83        self.assertEqual(a_list.count(1), 3)
    

    the test passes.

  • I want to see what happens when I try to count something that is not in the list.

    lists/tests/test_lists.py
    83        self.assertEqual(a_list.count(1), 3)
    84        self.assertEqual(a_list.count('not in list'), 3)
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: 0 != 3
    

    The count method returns 0 when the item is not in the list

  • I change the value to match

    lists/tests/test_lists.py
    84        self.assertEqual(a_list.count('not in list'), 0)
    

    the test passes.

  • I change the name of the test

    lists/tests/test_lists.py
    78        self.assertEqual(a_list.copy(), [0, 1, 2, 'n'])
    79
    80    def test_count_number_of_times_item_is_in_a_list(self):
    81        a_list = [0, 1, 2, 1, 'n', 1]
    82        self.assertEqual(a_list.count(0), 1)
    83        self.assertEqual(a_list.count(1), 3)
    84        self.assertEqual(a_list.count('not in list'), 0)
    85
    86
    87'count',
    
  • I remove count from the TODO list

    lists/tests/test_lists.py
    84        self.assertEqual(a_list.count('not in list'), 0)
    85
    86
    87'extend',
    88'index',
    89'insert',
    90'pop',
    91'remove',
    92'reverse',
    93'sort'
    
  • I add a git commit message

    terminal
    git commit -am \
    'add test_count_number_of_times_item_is_in_a_list'
    

I know how to count how many times something is in a list


test_extend_adds_items_from_an_iterable_to_end_of_a_list

RED: make it fail


I add a test for the extend method

lists/tests/test_lists.py
84        self.assertEqual(a_list.count('not in list'), 0)
85
86    def test_extend(self):
87        a_list = [0, 1, 2, 'n']
88        self.assertIsNone(a_list.extend())
89
90
91'extend',

the terminal is my friend, and shows TypeError

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

GREEN: make it pass


I pass a value to the call

lists/tests/test_lists.py
88        self.assertIsNone(a_list.extend(0))

the terminal is my friend, and shows TypeError

terminal
TypeError: 'int' object is not iterable

I change the value to an iterable

lists/tests/test_lists.py
88        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

    lists/tests/test_lists.py
    86    def test_extend(self):
    87        a_list = [0, 1, 2, 'n']
    88        self.assertIsNone(a_list.extend((0, 1)))
    89        self.assertEqual(a_list, [0, 1, 2, 'n'])
    90
    91
    92'extend',
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: Lists differ: [0, 1, 2, 'n', 0, 1]
                               != [0, 1, 2, 'n']
    
  • I change the expectation to match

    lists/tests/test_lists.py
    89        self.assertEqual(a_list, [0, 1, 2, 'n', 0, 1])
    

    the test passes.

  • I change the values given to the extend method

    lists/tests/test_lists.py
    86    def test_extend(self):
    87        a_list = [0, 1, 2, 'n']
    88        self.assertIsNone(a_list.extend((2, 1, 0)))
    89        self.assertEqual(a_list, [0, 1, 2, 'n', 0, 1])
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: Lists differ: [0, 1, 2, 'n', 2, 1, 0]
                               != [0, 1, 2, 'n', 0, 1]
    
  • I change the values to match

    lists/tests/test_lists.py
    89        self.assertEqual(a_list, [0, 1, 2, 'n', 2, 1, 0])
    

    the test is green again, it looks like extend calls append for each item in the iterable.

  • I add another assertion to see if the extend method does the same as using a for loop with the append method

    lists/tests/test_lists.py
    86    def test_extend(self):
    87        a_list = [0, 1, 2, 'n']
    88        self.assertIsNone(a_list.extend((2, 1, 0)))
    89        self.assertEqual(a_list, [0, 1, 2, 'n', 2, 1, 0])
    90
    91        another_list = [0, 1, 2, 'n']
    92        for number in (2, 1, 0):
    93            another_list.append(number)
    94        self.assertEqual(another_list, 'BOOM!!!')
    95
    96
    97'extend',
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: [0, 1, 2, 'n', 2, 1, 0] != 'BOOM!!!'
    
  • I change the expectation to a_list

    lists/tests/test_lists.py
    91        another_list = [0, 1, 2, 'n']
    92        for number in (2, 1, 0):
    93            another_list.append(number)
    94        self.assertEqual(another_list, a_list)
    95
    96
    97'extend',
    

    the test passes.

  • I change the name of the test

    lists/tests/test_lists.py
    84        self.assertEqual(a_list.count('not in list'), 0)
    85
    86    def test_extend_adds_items_from_an_iterable_to_end_of_a_list(self):
    87        a_list = [0, 1, 2, 'n']
    
  • I remove extend from the TODO list

    lists/tests/test_lists.py
     94        self.assertEqual(another_list, a_list)
     95
     96
     97'index',
     98'insert',
     99'pop',
    100'remove',
    101'reverse',
    102'sort'
    
  • I add a git commit message

    terminal
    git commit -am \
    'add test_extend_adds_items_from_an_iterable_to_end_of_a_list'
    

I know how to add items from an iterable to a list


test_index_returns_the_first_position_of_a_given_item

RED: make it fail


I add a test for the index method

lists/tests/test_lists.py
 94        self.assertEqual(another_list, a_list)
 95
 96    def test_index(self):
 97        a_list = [0, 1, 2, 'n']
 98        self.assertIsNone(a_list.index())
 99
100
101'index',

the terminal is my friend, and shows TypeError

terminal
TypeError: index expected at least 1 argument, got 0

GREEN: make it pass


I add a value to the call

lists/tests/test_lists.py
96    def test_index(self):
97        a_list = [0, 1, 2, 'n']
98        self.assertIsNone(a_list.index(0))

the terminal is my friend, and shows AssertionError

terminal
AssertionError: 0 is not None

I add the expectation

lists/tests/test_lists.py
98        self.assertIsNone(a_list.index(0), 0)

the terminal is my friend, and shows AssertionError

terminal
AssertionError: 0 is not None : 0

I change assertIsNone to the assertEqual method

lists/tests/test_lists.py
96    def test_index(self):
97        a_list = [0, 1, 2, 'n']
98        self.assertEqual(a_list.index(0), 0)

the test passes.


REFACTOR: make it better


  • I change the list to find to make sure the method did not return the same value I gave

    lists/tests/test_lists.py
    96    def test_index(self):
    97        a_list = ['1st', '2nd', '3rd', '...last']
    98        self.assertEqual(a_list.index(0), 0)
    

    the terminal is my friend, and shows ValueError

    terminal
    ValueError: 0 is not in list
    

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

  • I add ValueError to the list of Exceptions seen

    lists/tests/test_lists.py
    106'sort'
    107
    108
    109# Exceptions seen
    110# AssertionError
    111# TypeError
    112# ValueError
    
  • I remove the things around the call to the index method and change the value to make it clearer

    lists/tests/test_lists.py
    96    def test_index(self):
    97        a_list = ['1st', '2nd', '3rd', '...last']
    98        a_list.index('not in list')
    

    the terminal is my friend, and shows ValueError

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

    lists/tests/test_lists.py
     96    def test_index(self):
     97        a_list = ['1st', '2nd', '3rd', '...last']
     98
     99        with self.assertRaises(ValueError):
    100            a_list.index('not in list')
    

    the test is green again

  • I add a new assertion

    lists/tests/test_lists.py
     96    def test_index(self):
     97        a_list = ['1st', '2nd', '3rd', '...last']
     98        self.assertEqual(a_list.index('1st'), '1st')
     99
    100        with self.assertRaises(ValueError):
    101            a_list.index('not in list')
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: 0 != '1st'
    
  • I change the expectation

    lists/tests/test_lists.py
    98    self.assertEqual(a_list.index('1st'), 0)
    

    the test passes. The index method does not just return the value I give it

  • I add another assertion

    lists/tests/test_lists.py
     96    def test_index(self):
     97        a_list = ['1st', '2nd', '3rd', '...last']
     98        self.assertEqual(a_list.index('1st'), 0)
     99        self.assertEqual(a_list.index('3rd'), 0)
    100
    101        with self.assertRaises(ValueError):
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: 2 != 0
    
  • I change the expectation of the assertion

    lists/tests/test_lists.py
    99    self.assertEqual(a_list.index('3rd'), 2)
    

    the test passes.

  • I add another assertion

    lists/tests/test_lists.py
     99      self.assertEqual(a_list.index('3rd'), 2)
    100      self.assertEqual(a_list.index('2nd'), 2)
    101
    102      with self.assertRaises(ValueError):
    103          a_list.index('not in list')
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: 1 != 2
    
  • I change the expectation to match

    lists/tests/test_lists.py
    100        self.assertEqual(a_list.index('2nd'), 1)
    

    the test is green again

  • I add another assertion

    lists/tests/test_lists.py
    100        self.assertEqual(a_list.index('2nd'), 1)
    101        self.assertEqual(a_list.index('...last'), 1)
    102
    103        with self.assertRaises(ValueError):
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: 3 != 1
    
  • I change the expectation

    lists/tests/test_lists.py
    101        self.assertEqual(a_list.index('...last'), 3)
    

    the test passes.

  • I add something in the list again to see what happens if I have the same item in the list more than once

    lists/tests/test_lists.py
    96    def test_index(self):
    97        a_list = ['1st', '2nd', '3rd', '...last', '1st']
    

    the test is still green.

  • I add an assertion

    lists/tests/test_lists.py
    101        self.assertEqual(a_list.index('...last'), 3)
    102        self.assertEqual(a_list.index('1st'), 4)
    103
    104        with self.assertRaises(ValueError):
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: 0 != 4
    

    when I first called the index method, the terminal showed TypeError

    terminal
    TypeError: index expected at least 1 argument, got 0
    

    which means I should be able to send more than one argument.

  • I add a second argument

    lists/tests/test_lists.py
    102        self.assertEqual(a_list.index('1st', 0), 4)
    

    the terminal still shows AssertionError

    terminal
    AssertionError: 0 != 4
    
  • I change the argument

    lists/tests/test_lists.py
    102self.assertEqual(a_list.index('1st', 1), 4)
    

    the test passes, the second input is the position I want the index method to start from

  • I try the same thing with the other assertions

    lists/tests/test_lists.py
     96    def test_index(self):
     97        a_list = ['1st', '2nd', '3rd', '...last', '1st']
     98        self.assertEqual(a_list.index('1st', 0), 0)
     99        self.assertEqual(a_list.index('3rd', 0), 2)
    100        self.assertEqual(a_list.index('2nd', 0), 1)
    101        self.assertEqual(a_list.index('...last', 0), 3)
    102        self.assertEqual(a_list.index('1st', 1), 4)
    103
    104        with self.assertRaises(ValueError):
    105            a_list.index('not in list')
    

    the test is still green.

  • I change the name of the test

    lists/tests/test_lists.py
    94        self.assertEqual(another_list, a_list)
    95
    96    def test_index_returns_the_first_position_of_a_given_item(self):
    97        a_list = ['1st', '2nd', '3rd', '...last', '1st']
    
  • I remove index from the TODO list

    lists/tests/test_lists.py
    104        with self.assertRaises(ValueError):
    105            a_list.index('not in list')
    106
    107
    108'insert',
    109'pop',
    110'remove',
    111'reverse',
    112'sort'
    
  • I add a git commit message

    terminal
    git commit -am \
    'add test_index_returns_the_first_position_of_a_given_item'
    

I know how to find the position of something in a list.


test_insert_item_at_position_in_a_list

RED: make it fail


I add a test for the next method

lists/tests/test_lists.py
104        with self.assertRaises(ValueError):
105            a_list.index('not in list')
106
107    def test_insert(self):
108        a_list = [0, 1, 2, 'n']
109        self.assertIsNone(a_list.insert())
110
111
112'insert',

the terminal is my friend, and shows TypeError

terminal
TypeError: insert expected 2 arguments, got 0

GREEN: make it pass


I add two values to the call

lists/tests/test_lists.py
107    def test_insert(self):
108        a_list = [0, 1, 2, 'n']
109        self.assertIsNone(a_list.insert(0, 1))

the test passes.


REFACTOR: make it better


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

    lists/tests/test_lists.py
    107    def test_insert(self):
    108        a_list = [0, 1, 2, 'n']
    109        self.assertIsNone(a_list.insert(0, 1))
    110        self.assertEqual(a_list, [0, 1, 2, 'n'])
    111
    112
    113'insert',
    

    the terminal is my friend, and shows AssertionError

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

    there is an extra value at the beginning of the list.

  • I add the new value to the expectation

    lists/tests/test_lists.py
    110        self.assertEqual(a_list, [1, 0, 1, 2, 'n'])
    

    the test passes.

    The insert method

    • puts the second input given at the index of the list given as the first input

    • it also moves the original items from that index and after in the list, to the right

  • I change the second input in the call

    lists/tests/test_lists.py
    107    def test_insert(self):
    108        a_list = [0, 1, 2, 'n']
    109        self.assertIsNone(a_list.insert(0, -1))
    110        self.assertEqual(a_list, [0, 1, 2, 'n'])
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: Lists differ: [-1, 0, 1, 2, 'n']
                               != [0, 1, 2, 'n']
    
  • I change the expectation to match

    lists/tests/test_lists.py
    110        self.assertEqual(a_list, [-1, 0, 1, 2, 'n'])
    

    the test is green again

  • I add another assertion to see what happens when I insert an item in the middle of the list

    lists/tests/test_lists.py
    105    def test_insert(self):
    106        a_list = [0, 1, 2, 'n']
    107        self.assertIsNone(a_list.insert(0, -1))
    108        self.assertEqual(a_list, [-1, 0, 1, 2, 'n'])
    109        self.assertIsNone(a_list.insert(3, 1.5))
    110        self.assertEqual(a_list, [-1, 0, 1, 2, 'n'])
    111
    112
    113'insert',
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: Lists differ: [-1, 0, 1, 1.5, 2, 'n']
                               != [-1, 0, 1, 2, 'n']
    
  • I add the value to the expectation

    lists/tests/test_lists.py
    112        self.assertEqual(a_list, [-1, 0, 1, 1.5, 2, 'n'])
    

    the test passes. The insert method moved everything in the list from the index I gave and after, to the right.

  • I change the name of the test

    lists/tests/test_lists.py
    104        with self.assertRaises(ValueError):
    105            a_list.index('not in list')
    106
    107    def test_insert_item_at_position_in_a_list(self):
    108        a_list = [0, 1, 2, 'n']
    
  • I remove insert from the TODO list

    lists/tests/test_lists.py
    112        self.assertEqual(a_list, [-1, 0, 1, 1.5, 2, 'n'])
    113
    114
    115'pop',
    116'remove',
    117'reverse',
    118'sort'
    
  • I add a git commit message

    terminal
    git commit -am \
    'add test_insert_item_at_position_in_a_list'
    

I know how to put an item at a position in a list


test_pop_removes_and_returns_last_item_from_a_list

RED: make it fail


I add a test for the pop method

lists/tests/test_lists.py
112        self.assertEqual(a_list, [-1, 0, 1, 1.5, 2, 'n'])
113
114    def test_pop(self):
115        a_list = [0, 1, 2, 'n']
116        self.assertIsNone(a_list.pop())
117
118
119'pop',

the terminal is my friend, and shows AssertionError

terminal
AssertionError: 'n' is not None

The pop method returns the last item in the list.


GREEN: make it pass


I add the expectation

lists/tests/test_lists.py
111        self.assertIsNone(a_list.pop(), 'n')

the terminal is my friend, and shows AssertionError

terminal
AssertionError: 'n' is not None : n

I change assertIsNone to the assertEqual method

lists/tests/test_lists.py
114    def test_pop(self):
115        a_list = [0, 1, 2, 'n']
116        self.assertEqual(a_list.pop(), 'n')

the test passes.


REFACTOR: make it better


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

    lists/tests/test_lists.py
    114    def test_pop(self):
    115        a_list = [0, 1, 2, 'n']
    116        self.assertEqual(a_list.pop(), 'n')
    117        self.assertEqual(a_list, [0, 1, 2, 'n'])
    118
    119
    120'pop',
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: Lists differ: [0, 1, 2]
                               != [0, 1, 2, 'n']
    
  • I change the expectation of the assertion

    lists/tests/test_lists.py
    117        self.assertEqual(a_list, [0, 1, 2])
    

    the test passes.

  • I add another assertion with the pop method

    lists/tests/test_lists.py
    114    def test_pop(self):
    115        a_list = [0, 1, 2, 'n']
    116        self.assertEqual(a_list.pop(), 'n')
    117        self.assertEqual(a_list, [0, 1, 2])
    118        self.assertEqual(a_list.pop(), 'n')
    119
    120
    121'pop',
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: 2 != 'n'
    

    The pop method returns the last item in the list.

  • I change the expectation of the assertion

    lists/tests/test_lists.py
    118        self.assertEqual(a_list.pop(), 2)
    

    the test passes.

  • I add another assertion to see what changed in the list

    lists/tests/test_lists.py
    114    def test_pop(self):
    115        a_list = [0, 1, 2, 'n']
    116        self.assertEqual(a_list.pop(), 'n')
    117        self.assertEqual(a_list, [0, 1, 2])
    118        self.assertEqual(a_list.pop(), 2)
    119        self.assertEqual(a_list, [0, 1, 2])
    120
    121
    122'pop',
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: Lists differ: [0, 1] != [0, 1, 2]
    
  • I change the expectation of the assertion

    lists/tests/test_lists.py
    119        self.assertEqual(a_list, [0, 1])
    

    the test passes.

  • I change the name of the test

    lists/tests/test_lists.py
    112        self.assertEqual(a_list, [-1, 0, 1, 1.5, 2, 'n'])
    113
    114    def test_pop_removes_and_returns_last_item_from_a_list(self):
    115        a_list = [0, 1, 2, 'n']
    116        self.assertEqual(a_list.pop(), 'n')
    
  • I take out pop from the TODO list

    lists/tests/test_lists.py
    119        self.assertEqual(a_list, [0, 1])
    120
    121
    122'remove',
    123'reverse',
    124'sort'
    
  • I add a git commit message

    terminal
    git commit -am \
    'add test_pop_removes_and_returns_last_item_from_a_list'
    

I know how to get and remove the last item from a list.


test_remove_first_time_item_is_in_a_list

RED: make it fail


time for the remove method

lists/tests/test_lists.py
119        self.assertEqual(a_list, [0, 1])
120
121    def test_remove(self):
122        a_list = [0, 1, 2, 'n']
123        self.assertIsNone(a_list.remove())
124
125
126'remove',

the terminal is my friend, and shows TypeError

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

GREEN: make it pass


I add a value to the call

lists/tests/test_lists.py
123        self.assertIsNone(a_list.remove(0))

the test is green again. The remove method returns None.


REFACTOR: make it better


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

    lists/tests/test_lists.py
    121    def test_remove(self):
    122        a_list = [0, 1, 2, 'n']
    123        self.assertIsNone(a_list.remove(0))
    124        self.assertEqual(a_list, [0, 1, 2, 'n'])
    125
    126
    127'remove',
    

    the terminal is my friend, and shows AssertionError

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

    the remove method removes the item given as input, from the list.

  • I change the expectation

    lists/tests/test_lists.py
    124        self.assertEqual(a_list, [1, 2, 'n'])
    

    the test passes.

  • I change the values in the list to see what happens when an item is in there more than one time

    lists/tests/test_lists.py
    121    def test_remove(self):
    122        a_list = [0, 1, 0, 2, 0, 'n']
    123        self.assertIsNone(a_list.remove(0))
    124        self.assertEqual(a_list, [1, 2, 'n'])
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: Lists differ: [1, 0, 2, 0, 'n'] != [1, 2, 'n']
    
  • I change the values to match

    lists/tests/test_lists.py
    119        self.assertEqual(a_list, [1, 0, 2, 0, 'n'])
    

    the test passes. The remove method only removes the first time something shows up in the list.

  • I want to see what happens when I try to remove something that is not in the list

    lists/tests/test_lists.py
    119    def test_remove(self):
    120        a_list = [0, 1, 0, 2, 0, 'n']
    121        self.assertIsNone(a_list.remove(0))
    122        self.assertEqual(a_list, [1, 0, 2, 0, 'n'])
    123        self.assertIsNone(a_list.remove('not in list'))
    124
    125
    126'remove',
    

    the terminal is my friend, and shows ValueError

    terminal
    ValueError: list.remove(x): x not in list
    
  • I remove the things around the call then add assertRaises

    lists/tests/test_lists.py
    121    def test_remove(self):
    122        a_list = [0, 1, 0, 2, 0, 'n']
    123        self.assertIsNone(a_list.remove(0))
    124        self.assertEqual(a_list, [1, 0, 2, 0, 'n'])
    125
    126        with self.assertRaises(ValueError):
    127            a_list.remove('not in list')
    128
    129
    130'remove',
    

    the test passes.

  • I change the name of the test

    lists/tests/test_lists.py
    119        self.assertEqual(a_list, [0, 1])
    120
    121    def test_remove_first_time_item_is_in_a_list(self):
    122        a_list = [0, 1, 0, 2, 0, 'n']
    123        self.assertIsNone(a_list.remove(0))
    
  • I take out remove from the TODO list

    lists/tests/test_lists.py
    126        with self.assertRaises(ValueError):
    127            a_list.remove('not in list')
    128
    129
    130'reverse',
    131'sort'
    
  • I add a git commit message

    terminal
    git commit -am \
    'add test_remove_first_time_item_is_in_a_list'
    

I know how to remove the first time something is in a list.


test_reverse_a_list

RED: make it fail


  • I add a test for the reverse method

    lists/tests/test_lists.py
    126        with self.assertRaises(ValueError):
    127            a_list.remove('not in list')
    128
    129    def test_reverse(self):
    130        a_list = [0, 1, 2, 'n']
    131        self.assertIsNone(a_list.reverse())
    132
    133
    134'reverse',
    

    the reverse method returns None.

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

    lists/tests/test_lists.py
    129    def test_reverse(self):
    130        a_list = [0, 1, 2, 'n']
    131        self.assertIsNone(a_list.reverse())
    132        self.assertEqual(a_list, [0, 1, 2, 'n'])
    133
    134
    135'reverse',
    

    the terminal is my friend, and shows AssertionError

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

    the reverse method reversed the order of the items in the list.


GREEN: make it pass


I change the expectation of the assertion

lists/tests/test_lists.py
132        self.assertEqual(a_list, ['n', 2, 1, 0])

the test passes.


REFACTOR: make it better


  • I change the name of the test

    lists/tests/test_lists.py
    126        with self.assertRaises(ValueError):
    127            a_list.remove('not in list')
    128
    129    def test_reverse_a_list(self):
    130        a_list = [0, 1, 2, 'n']
    
  • I remove reverse from the TODO list

    lists/tests/test_lists.py
    132        self.assertEqual(a_list, ['n', 2, 1, 0])
    133
    134
    135'sort'
    136
    137
    138# Exceptions seen
    
  • I add a git commit message

    terminal
    git commit -am 'add test_reverse_a_list'
    

I know how to reverse a list


test_sort_a_list

RED: make it fail


I add a test for the sort method

lists/tests/test_lists.py
132        self.assertEqual(a_list, ['n', 2, 1, 0])
133
134    def test_sort(self):
135        a_list = [0, 1, 2, 'n']
136        self.assertIsNone(a_list.sort())
137
138
139'sort'

the terminal is my friend, and shows TypeError

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

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


GREEN: make it pass


I remove the things around the call then add assertRaises

lists/tests/test_lists.py
134    def test_sort(self):
135        a_list = [0, 1, 2, 'n']
136        with self.assertRaises(TypeError):
137            a_list.sort()
138
139
140'sort'

the test passes.


REFACTOR: make it better


  • I can use the list directly in the call

    lists/tests/test_lists.py
    134  def test_sort(self):
    135      a_list = [0, 1, 2, 'n']
    136      with self.assertRaises(TypeError):
    137          [0, 1, 2, 'n'].sort()
    

    the test is still green.

  • I remove the variable because it is no longer used

    lists/tests/test_lists.py
    134    def test_sort(self):
    135        with self.assertRaises(TypeError):
    136            [0, 1, 2, 'n'].sort()
    

    still green.

  • I add a new list that has only numbers, then add another assertion

    lists/tests/test_lists.py
    134    def test_sort(self):
    135        with self.assertRaises(TypeError):
    136            [0, 1, 2, 'n'].sort()
    137
    138        a_list = [0, 1, 2, 3]
    139        self.assertIsNone(a_list.sort())
    140
    141
    142'sort'
    

    the test is still green. The sort method returns None when called.

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

    lists/tests/test_lists.py
    138        a_list = [0, 1, 2, 3]
    139        self.assertIsNone(a_list.sort())
    140        self.assertEqual(a_list, [])
    141
    142
    143'sort'
    

    the terminal is my friend, and shows AssertionError

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

    the list stayed the same.

  • I change the expectation to match

    lists/tests/test_lists.py
    140        self.assertEqual(a_list, [0, 1, 2, 3])
    

    the test passes. The name of the method is sort and I gave it a list that is sorted.

  • I change the list to see what happens when it is NOT sorted

    lists/tests/test_lists.py
    134    def test_sort(self):
    135        with self.assertRaises(TypeError):
    136            [0, 1, 2, 'n'].sort()
    137
    138        a_list = [0, 1, -1, 2, -2, 3, -3]
    139        self.assertIsNone(a_list.sort())
    140        self.assertEqual(a_list, [0, 1, 2, 3])
    

    the terminal is my friend, and shows AssertionError

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

    the sort method changed the order of the list from smallest to biggest number.

  • I change the expectation of the assertion to match

    lists/tests/test_lists.py
    140        self.assertEqual(a_list, [-3, -2, -1, 0, 1, 2, 3])
    

    the test passes.

  • I change the name of the test

    lists/tests/test_lists.py
    132        self.assertEqual(a_list, ['n', 2, 1, 0])
    133
    134    def test_sort_a_list(self):
    135        with self.assertRaises(TypeError):
    136            [0, 1, 2, 'n'].sort()
    
  • I remove sort from the TODO list

    lists/tests/test_lists.py
    140        self.assertEqual(a_list, [-3, -2, -1, 0, 1, 2, 3])
    141
    142
    143# Exceptions seen
    144# AssertionError
    145# TypeError
    146# ValueError
    
  • I add a git commit message

    terminal
    git commit -am 'add test_sort_a_list'
    

I know how to sort a list.


test_getting_items_of_a_list

When I want an item that is in a list, I can give its index in square brackets([]) to get it.


RED: make it fail


I add a test for getting items of a list

lists/tests/test_lists.py
140        self.assertEqual(a_list, [-3, -2, -1, 0, 1, 2, 3])
141
142    def test_getting_items_of_a_list(self):
143        a_list = ['1st', '2nd', '3rd', '...last']
144        self.assertEqual(a_list[0], '')
145
146
147# Exceptions seen

the terminal is my friend, and shows AssertionError

terminal
AssertionError: '1st' != ''

the first item has an index of 0.


GREEN: make it pass


I change the value of the expectation

lists/tests/test_lists.py
144        self.assertEqual(a_list[0], '1st')

the test passes.


REFACTOR: make it better


  • this is the opposite of the index method which takes in the item and returns its position, in this case I provide the index (position) and it returns the item, which means I can write this

    lists/tests/test_lists.py
    142    def test_getting_items_of_a_list(self):
    143        a_list = ['1st', '2nd', '3rd', '...last']
    144        self.assertEqual(a_list[0], '1st')
    145        self.assertEqual(a_list[a_list.index('1st')], '')
    146
    147
    148# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: '1st' != ''
    
  • I change the expectation to match

    lists/tests/test_lists.py
    145        self.assertEqual(a_list[a_list.index('1st')], '1st')
    

    the test passes because

    • a_list.index('1st') returns 0

    • a_list[0] returns 1st

  • 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

    lists/tests/test_lists.py
    145        self.assertEqual(a_list[a_list.index('1st')], '1st')
    146        self.assertEqual(a_list[-4], '')
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: '1st' != ''
    
  • I change the expectation to match

    lists/tests/test_lists.py
    146        self.assertEqual(a_list[-4], '1st')
    

    the test passes.

  • I add another assertion

    lists/tests/test_lists.py
    146        self.assertEqual(a_list[-4], '1st')
    147        self.assertEqual(a_list[2], '')
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: '3rd' != ''
    
  • I change the expectation to match

    lists/tests/test_lists.py
    147        self.assertEqual(a_list[2], '3rd')
    

    the test is green again

  • I add another assertion

    lists/tests/test_lists.py
    147        self.assertEqual(a_list[2], '3rd')
    148        self.assertEqual(a_list[-2], '')
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: '3rd' != ''
    
  • I change the expectation

    lists/tests/test_lists.py
    148        self.assertEqual(a_list[-2], '3rd')
    

    the test passes.

  • I add another assertion

    lists/tests/test_lists.py
    148        self.assertEqual(a_list[-2], '3rd')
    149        self.assertEqual(a_list[1], '')
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: '2nd' != ''
    
  • I change the expectation

    lists/tests/test_lists.py
    149        self.assertEqual(a_list[1], '2nd')
    

    the test passes.

  • I add another assertion

    lists/tests/test_lists.py
    149        self.assertEqual(a_list[1], '2nd')
    150        self.assertEqual(a_list[-3], '')
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: '2nd' != ''
    
  • I change the expectation

    lists/tests/test_lists.py
    150        self.assertEqual(a_list[-3], '2nd')
    

    the test passes.

  • I add another assertion

    lists/tests/test_lists.py
    150        self.assertEqual(a_list[-3], '2nd')
    151        self.assertEqual(a_list[3], '')
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: '...last' != ''
    
  • I change the expectation

    lists/tests/test_lists.py
    151        self.assertEqual(a_list[3], '...last')
    

    the test passes.

  • I add another assertion

    lists/tests/test_lists.py
    151        self.assertEqual(a_list[3], '...last')
    152        self.assertEqual(a_list[-1], '')
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: '...last' != ''
    
  • I make the expectation match

    lists/tests/test_lists.py
    142    def test_getting_items_of_a_list(self):
    143        a_list = ['1st', '2nd', '3rd', '...last']
    144        self.assertEqual(a_list[0], '1st')
    145        self.assertEqual(a_list[a_list.index('1st')], '1st')
    146        self.assertEqual(a_list[-4], '1st')
    147        self.assertEqual(a_list[2], '3rd')
    148        self.assertEqual(a_list[-2], '3rd')
    149        self.assertEqual(a_list[1], '2nd')
    150        self.assertEqual(a_list[-3], '2nd')
    151        self.assertEqual(a_list[3], '...last')
    152        self.assertEqual(a_list[-1], '...last')
    153
    154
    155# Exceptions seen
    

    the test passes.

  • I add a git commit message

    terminal
    git commit -am \
    'add test_getting_items_of_a_list'
    

I know how to get things that are in a list.


test_setting_items_in_a_list

I can also use the index of an item in a list to change the item.


RED: make it fail


I add a test for setting items in a list

lists/tests/test_lists.py
152        self.assertEqual(a_list[-1], '...last')
153
154    def test_setting_items_in_a_list(self):
155        a_list = ['1st', '2nd', '3rd', '...last']
156        a_list[-1] = '4th'
157        self.assertEqual(
158            a_list, ['1st', '2nd', '3rd', '...last']
159        )
160
161
162# Exceptions seen

the terminal is my friend, and shows AssertionError

terminal
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 the way I point a name to a value when I make a variable.


GREEN: make it pass


  • I change the expectation to match the values in the terminal

    lists/tests/test_lists.py
    157        self.assertEqual(
    158            a_list, ['1st', '2nd', '3rd', '4th']
    159        )
    160
    161
    162# Exceptions seen
    

    the test passes.

  • I add a git commit message

    terminal
    git commit -am \
    'add test_setting_items_in_a_list'
    

I know how to change the value of an item in a list.


test_getting_parts_of_a_list_aka_slicing

I can use indices to look at parts of a list, this is called slicing.


RED: make it fail


I add a test for looking at parts of a list

lists/tests/test_lists.py
157        self.assertEqual(
158            a_list, ['1st', '2nd', '3rd', '4th']
159        )
160
161    def test_getting_parts_of_a_list(self):
162        a_list = ['a', 'b', 'c', 'd']
163        self.assertEqual(a_list[0:2], [])
164
165
166# Exceptions seen

the terminal is my friend, and shows AssertionError

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

GREEN: make it pass


I change the expectation to match

lists/tests/test_lists.py
163        self.assertEqual(a_list[0:2], ['a', 'b'])

the test passes.

I give two values in square brackets([]), separated by a :


REFACTOR: make it better


  • I can skip the first number if the starting index is 0

    lists/tests/test_lists.py
    161    def test_getting_parts_of_a_list(self):
    162        a_list = ['a', 'b', 'c', 'd']
    163        self.assertEqual(a_list[0:2], ['a', 'b'])
    164        self.assertEqual(a_list[:2], [])
    165
    166
    167# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: Lists differ: ['a', 'b'] != []
    
  • I change the expectation to match

    lists/tests/test_lists.py
    164        self.assertEqual(a_list[:2], ['a', 'b'])
    

    the test is green again. [0:2] and [:2] are the same.

  • I add another assertion

    lists/tests/test_lists.py
    164        self.assertEqual(a_list[:2], ['a', 'b'])
    165        self.assertEqual(a_list[1:4], [])
    166
    167
    168# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: Lists differ: ['b', 'c', 'd'] != []
    
  • I add the missing values

    lists/tests/test_lists.py
    165        self.assertEqual(a_list[1:4], ['b', 'c', 'd'])
    

    the test passes.

  • I can skip the second number when it is bigger than or the same as the number of things in the list

    lists/tests/test_lists.py
    165        self.assertEqual(a_list[1:4], ['b', 'c', 'd'])
    166        self.assertEqual(a_list[1:], [])
    167
    168
    169# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: Lists differ: ['b', 'c', 'd'] != []
    
  • I add the missing values

    lists/tests/test_lists.py
    166        self.assertEqual(a_list[1:], ['b', 'c', 'd'])
    

    the test passes. [1:4] and [1:] are the same because this list has 4 things.

  • I add another assertion

    lists/tests/test_lists.py
    166        self.assertEqual(a_list[1:], ['b', 'c', 'd'])
    167        self.assertEqual(a_list[0:3], [])
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: Lists differ: ['a', 'b', 'c'] != []
    
  • I change the expectation

    lists/tests/test_lists.py
    167        self.assertEqual(a_list[0:3], ['a', 'b', 'c'])
    

    the test passes.

  • I add another assertion

    lists/tests/test_lists.py
    167        self.assertEqual(a_list[0:3], ['a', 'b', 'c'])
    168        self.assertEqual(a_list[1:3], [])
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: Lists differ: ['b', 'c'] != []
    
  • I change the expectation

    lists/tests/test_lists.py
    168        self.assertEqual(a_list[1:3], ['b', 'c'])
    

    the test passes.

  • I can also skip the starting and ending numbers

    lists/tests/test_lists.py
    168        self.assertEqual(a_list[1:3], ['b', 'c'])
    169        self.assertEqual(a_list[:], [])
    

    the terminal is my friend, and shows AssertionError

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

    I get the entire list back or a copy

    lists/tests/test_lists.py
    169        self.assertEqual(a_list[:], a_list.copy())
    

    the test passes.

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

    lists/tests/test_lists.py
    157        self.assertEqual(
    158            a_list, ['1st', '2nd', '3rd', '4th']
    159        )
    160
    161    def test_getting_parts_of_a_list_aka_slicing(self):
    162        a_list = ['a', 'b', 'c', 'd']
    
  • I add a git commit message

    terminal
    git commit -am \
    'add test_getting_parts_of_a_list_aka_slicing'
    

I know how to look at parts of a list.


test_index_error

IndexError is raised when I try to get an item from a list and use a number that points to something that is NOT in the list.


RED: make it fail


I add a test for IndexError

lists/tests/test_lists.py
169        self.assertEqual(a_list[:], a_list.copy())
170
171    def test_index_error(self):
172        a_list = ['a', 'b', 'c', 'd']
173        a_list[4]
174
175
176# Exceptions seen

the terminal is my friend, and shows IndexError

terminal
IndexError: list index out of range

when I use an index that is greater than or the same as the number of things in the list, I am pointing to something that is NOT in the list.


GREEN: make it pass


I add assertRaises

lists/tests/test_lists.py
171    def test_index_error(self):
172        a_list = ['a', 'b', 'c', 'd']
173
174        with self.assertRaises(IndexError):
175            a_list[4]

the test passes.


REFACTOR: make it better


  • I add IndexError to the list of Exceptions seen

    lists/tests/test_lists.py
    178# Exceptions seen
    179# AssertionError
    180# TypeError
    181# ValueError
    182# IndexError
    
  • I get IndexError when I use a number that is smaller than the number of things in the list as a negative number

    lists/tests/test_lists.py
    174        with self.assertRaises(IndexError):
    175            a_list[4]
    176        a_list[-5]
    

    the terminal is my friend, and shows IndexError

    terminal
    IndexError: list index out of range
    
  • I add the assertRaises method

    lists/tests/test_lists.py
    174        with self.assertRaises(IndexError):
    175            a_list[4]
    176        with self.assertRaises(IndexError):
    177            a_list[-5]
    178
    179
    180# Exceptions seen
    

    the test passes.

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

    lists/tests/test_lists.py
    174        with self.assertRaises(IndexError):
    175            a_list[4]
    176        with self.assertRaises(IndexError):
    177            a_list[-5]
    178        [].pop()
    179
    180
    181# Exceptions seen
    

    the terminal is my friend, and shows IndexError

    terminal
    IndexError: pop from empty list
    
  • I add assertRaises

    lists/tests/test_lists.py
    176        with self.assertRaises(IndexError):
    177            a_list[-5]
    178        with self.assertRaises(IndexError):
    179            [].pop()
    180
    181
    182# Exceptions seen
    

    the test passes.

  • I cannot remove the last item from a list that has no items, this is the same as trying to get an item from a list that has no items

    lists/tests/test_lists.py
    178        with self.assertRaises(IndexError):
    179            [].pop()
    180        [][-1]
    181
    182
    183# Exceptions seen
    

    the terminal is my friend, and shows IndexError

    terminal
    IndexError: list index out of range
    
  • I add assertRaises

    lists/tests/test_lists.py
    178        with self.assertRaises(IndexError):
    179            [].pop()
    180        with self.assertRaises(IndexError):
    181            [][-1]
    182
    183
    184# Exceptions seen
    

    the test passes. Any index given to an empty list raises IndexError.

  • I add a git commit message

    terminal
    git commit -am 'add test_index_error'
    

I know what causes IndexError.


test_dir_list with extend and sort

The attributes and methods of a list look like the attributes and methods of ‘object’ with extra things, and since they are both lists, I should be able to make the attributes and methods of a list by adding the “extra” to the attributes and methods of ‘object’.


RED: make it fail


I add a variable for the attributes and methods of a list, then add an assertion for it, in test_dir_list

lists/tests/test_lists.py
10      def test_dir_list(self):
11          self.maxDiff = None
12
13          dir_list = dir(object)
14          dir_list.extend((
15              'append', 'clear', 'copy',
16              'count', 'extend', 'index',
17              'insert', 'pop', 'remove',
18              'reverse', 'sort',
19          ))
20          self.assertEqual(dir(list), dir_list)
21
22          self.assertEqual(
23              dir(list),
24              [
25                  '__add__',

the terminal is my friend, and shows AssertionError

terminal
AssertionError: Lists differ: [
    '__add__', '__class__', '__class_getitem__',[549 chars]ort'
] != [
    '__class__', '__delattr__', '__dir__', '__do[370 chars]ort'
]

with a note

terminal
First list contains 13 additional elements.

I am close, but not quite.


GREEN: make it pass


  • I use extend to append the thirteen additional elements

    lists/tests/test_lists.py
    14        dir_list.extend((
    15            '__add__', '__class_getitem__',
    16            '__contains__', '__delitem__',
    17            '__getitem__', '__iadd__',
    18            '__imul__', '__iter__', '__len__',
    19            '__mul__', '__reversed__',
    20            '__rmul__', '__setitem__',
    21            'append', 'clear', 'copy',
    22            'count', 'extend', 'index',
    23            'insert', 'pop', 'remove',
    24            'reverse', 'sort',
    25        ))
    

    the terminal does not feel like my friend, it shows AssertionError. The details show what I added as missing and as extra. How can it be both? It looks like the problem is the position of the extra items.

  • I add a call to the sort method

    lists/tests/test_lists.py
    14        dir_list.extend((
    15            '__add__', '__class_getitem__',
    16            '__contains__', '__delitem__',
    17            '__getitem__', '__iadd__',
    18            '__imul__', '__iter__', '__len__',
    19            '__mul__', '__reversed__',
    20            '__rmul__', '__setitem__',
    21            'append', 'clear', 'copy',
    22            'count', 'extend', 'index',
    23            'insert', 'pop', 'remove',
    24            'reverse', 'sort',
    25        ))
    26        dir_list.sort()
    27        self.assertEqual(dir(list), dir_list)
    28
    29        self.assertEqual(
    30            dir(list),
    31            [
    32                '__add__',
    33                '__class__',
    

    the test passes, a reminder that all objects inherit from ‘object’.


REFACTOR: make it better


  • I remove the other assertion from test_dir_list

    lists/tests/test_lists.py
    26        dir_list.sort()
    27        self.assertEqual(dir(list), dir_list)
    28
    29    def test_append_adds_item_to_end_of_a_list(self):
    
  • I add a git commit message

    terminal
    git commit -am \
    'test_dir_list with extend and sort'
    

All objects inherit attributes and methods from ‘object’.


close the project

  • I close test_lists.py

  • I click in the terminal where the tests are running

  • I use q on the keyboard to leave the tests

  • I change directory to the parent of lists

    terminal
    cd ..
    

review

I ran tests to show that I can make a list with list() or square brackets ([]), then I ran these tests for the methods of lists from append to sort

I also added tests for


How many questions can you answer after going through this chapter?


code from the chapter

Do you want to see all the CODE I typed in this chapter?


what is next?

Would you like to refactor the attributes of the person object and instance with lists?


rate pumping python

If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too.