Search code examples
pythonlisttuples

Why is it saying my list is a tuple when I try to append?


I am trying to append something to an empty list but and error comes up saying that it doesn't not work on a tuple although it is a list.

Here is the code

l = []
u = [31536000, 86400, 3600, 60]
z = 157310805

for i in u:
    l.append(z), z %= i

the error says this:

Traceback (most recent call last):
  File "/workspace/default/tests.py", line 2, in <module>
    from solution import format_duration
  File "/workspace/default/solution.py", line 10
    l.append(z), z %= i
    ^^^^^^^^^^^^^^
SyntaxError: 'tuple' is an illegal expression for augmented assignment

Solution

  • You're creating a tuple by putting z %= i on the same line, separated with ,. This is an invalid statement because an assignment can't be used as an expression in a tuple.

    You should put each statement on its own line.

    for i in u:
        l.append(z)
        z %= i
    

    If you really need to put multiple statements on the same line, you can use ; to separate them.

    for i in u:
        l.append(z); z %= i