Lists Debugging#
Each of the following short code contains one or more bugs.
Please identify and correct these bugs.
Provide an explanation for your answer.
Question-1#
x = ['A' 'B' 'C' 'D' 'E']
Solution
Elements must be comma-separated.
Question-2#
x = ['A', 'B', 'C', 'D', 'E']
x(2)
Solution
Indexes must be inside square brackets. x(2) must be x[2].
Question-3#
x = ['A', 'B', 'C', 'D', 'E']
x[len(x)]
Solution
There is no element of the index len(x) since indexing starts from 0. The largest index is len(x)-1.
Question-4#
mylist = [['NY', 'CA', 'FL'], ('Amy', 'John', 'Ashley', 'Michael')]
mylist[0][3]
Solution
mylist[0] is the index of 0 element of mylist, which is [‘NY’, ‘CA’, ‘FL’].
mylist[0][3] is attempting to access the index 3 of [‘NY’, ‘CA’, ‘FL’], but that element does not exist.
Question-5#
mylist = [['NY', 'CA', 'FL'], ('Amy', 'John', 'Ashley', 'Michael')]
mylist[1][5]
Solution
mylist[1] is the index of 1 element of mylist which is (‘Amy’, ‘John’, ‘Ashley’, ‘Michael’).
mylist[1][5] is the index of 5 element of (‘Amy’, ‘John’, ‘Ashley’, ‘Michael’) which does not exist.