Python Forum
Problem between list and tuple
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem between list and tuple
#1
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
Reply
#2
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]
>>>
Reply
#3
Oh yes you're wright! Wrong indentation! Sorry that was a beginner mistake, Thank you ! :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using > < for tuple , list,... akbarza 3 402 Feb-05-2024, 01:18 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Change font in a list or tuple apffal 4 2,635 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  search a list or tuple for a specific type ot class Skaperen 8 1,855 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  Problem with "Number List" problem on HackerRank Pnerd 5 2,034 Apr-12-2022, 12:25 AM
Last Post: Pnerd
  why is my list a tuple CompleteNewb 7 2,214 Mar-17-2022, 10:09 PM
Last Post: CompleteNewb
  in a list or tuple Skaperen 6 78,571 May-16-2021, 09:59 PM
Last Post: Skaperen
  Create SQLite columns from a list or tuple? snakes 6 8,525 May-04-2021, 12:06 PM
Last Post: snakes
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,260 Jan-30-2021, 07:11 AM
Last Post: alloydog
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,729 Nov-04-2020, 11:26 AM
Last Post: Aggam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020