By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

tuple editing problem

sangeeth
P: 23
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
Jan 17 '07 #1
Share this Question
Share on Google+
2 Replies


P: 94
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 :
Expand|Select|Wrap|Line Numbers
  1. list = [1,2,3]
  2. print list
  3. ind = input("Enter new item (number) : ")
  4. list.append(ind)
  5. print list
  6.  
Jan 17 '07 #2

bvdet
Expert Mod 2.5K+
P: 2,851
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:
Expand|Select|Wrap|Line Numbers
  1. >>> tup = (1,2,3,())
  2. >>> tup
  3. (1, 2, 3, ())
  4. >>> tup = (tup[0], tup[1], tup[2], 4)
  5. >>> tup
  6. (1, 2, 3, 4)
  7. >>> 
  8. >>> 
  9. >>> lst = []
  10. >>> for i in tup:
  11. ...     lst.append(i)
  12. ...     
  13. >>> lst.append(5)
  14. >>> lst
  15. [1, 2, 3, 4, 5]
  16. >>> tup = tuple(lst)
  17. >>> tup
  18. (1, 2, 3, 4, 5)
  19. >>> 
Jan 17 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.