When we append the list's length to itself, what value would be appended?


#1

Question

In this code challenge, we are appending the list’s length to itself. If we append the list’s length to itself, what value would be appended, the previous length or the length after the value was appended?

Answer

Following the order of operations, this will calculate the list’s length first, then append that value to it.

To explain what this means, say we had a list of elements
list = [10, 20, 30].

If we call this method on the list,
.append(len(list))

because of operator precedence, the expression inside parentheses () will be evaluated first. This will evaluate to the current length of the list, 3, before it is appended, and essentially become the same as running this,
.append(3)

which would update the list to
[10, 20, 30, 3]

If, instead, the .append() happened first, then the value of len(list) might have been 4 instead, but because of operator precedence, this issue will not occur.