tuple editing problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sangeeth
    New Member
    • Dec 2006
    • 23

    tuple editing problem

    I have a tuple

    tup = (1,2,3,())

    I want to put a value 4 in the last position of tup
    i.e. i want tup to be (1,2,3,4)

    how can i do that
  • Extremist
    New Member
    • Jan 2007
    • 94

    #2
    Originally posted by sangeeth
    I have a tuple

    tup = (1,2,3,())

    I want to put a value 4 in the last position of tup
    i.e. i want tup to be (1,2,3,4)

    how can i do that
    Well as far as I know Tuples are fixed 'lists'
    You can't add, or delete from them eg The months of the year
    Rather use a list :
    Code:
    list = [1,2,3]
    print list
    ind = input("Enter new item (number) : ")
    list.append(ind)
    print list

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by sangeeth
      I have a tuple

      tup = (1,2,3,())

      I want to put a value 4 in the last position of tup
      i.e. i want tup to be (1,2,3,4)

      how can i do that
      Extremist is correct - tuples are immutable. You could do this however:
      Code:
      >>> tup = (1,2,3,())
      >>> tup
      (1, 2, 3, ())
      >>> tup = (tup[0], tup[1], tup[2], 4)
      >>> tup
      (1, 2, 3, 4)
      >>> 
      >>> 
      >>> lst = []
      >>> for i in tup:
      ... 	lst.append(i)
      ... 	
      >>> lst.append(5)
      >>> lst
      [1, 2, 3, 4, 5]
      >>> tup = tuple(lst)
      >>> tup
      (1, 2, 3, 4, 5)
      >>>

      Comment

      Working...