Python Forum
Problem between list and tuple - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problem between list and tuple (/thread-11645.html)



Problem between list and tuple - PierreSoulier - Jul-19-2018

Hello here is my code:

n=int(x[4])
        chiffres = []
        while n > 0:
            chiffres.append(n % 10)
            n = n // 10
            chiffres = tuple(reversed(chiffres))
            pc = []
        for i in range (0,2):
            pc.append(chiffres[i])
        test = ''.join(str(e) for e in pc)
        print(test)
And i get this error

Traceback (most recent call last):
  File "main.py", line 614, in <module>
    chiffres.append(n % 10)
AttributeError: 'tuple' object has no attribute 'append'
[] is not to declare a list? I'm confused

Just to mention, if I add chiffres = list(chiffres) between line 2 and 3 I still get the error


RE: Problem between list and tuple - Larz60+ - Jul-19-2018

tuples are immutable, thus append is not available.
on line 6, you convert the type chiffres from list to tuple use:
chiffres = chiffres[::-1]
>>> chiffres  = [1,3,6,8]
>>> chiffres
[1, 3, 6, 8]
>>> chiffres = chiffres[::-1]
>>> chiffres
[8, 6, 3, 1]
>>>



RE: Problem between list and tuple - PierreSoulier - Jul-19-2018

Oh yes you're wright! Wrong indentation! Sorry that was a beginner mistake, Thank you ! :)