Results 1 to 6 of 6

Thread: How to insert value into tuple?

  1. #1
    Join Date
    Aug 2009
    Posts
    59

    How to insert value into tuple?

    Hello friends,
    I am last year Computer Science student. I recently started learning python language. I know that tuples are immutable, but in one of my code I want to insert an extra value into each tuple. For example one item is the amount and I want to place a new item next to it in a different currency like below:
    ('ToothBrush', '4.00', '1000.00'). Can anyone tell me how to insert value into tuple? Please help me.
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to insert value into tuple?

    From your code it seems that you are tried to not updating a tuple and that's why you are getting such type of problem. You are tried to createa new tuple from an old tuple. In this case you have to use collections.namedtuple to create new type of object from an old type of object. By using collections.namedtuple class you will able to insert an extra value into each tuple.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to insert value into tuple?

    You have to use cast that value to fix this problem. You have to first cast that value in to a list, insert the item, then cast it back to a tuple. You can do this in following ways.
    Code:
    x = ('Toothbrush', '400.00', '1000.00')
    x = list(x)
    x.insert(3, 'foobar')
    x = tuple(x)
    print a
    
    >> ('Product', '400.00', '1000.00', 'foobar')

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to insert value into tuple?

    As per my information tuples are immutable and due to this you are able to insert new value to each new tuple. Due to immutable feature of tuple, you have to place new data back where you got the old one. You can do this using following code:
    Code:
    somestuple + (somesitem,)

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to insert value into tuple?

    You can add new value to tuple in following way. I have written following program for you. It is very simple program. In the following code I have use "x" variable to store data.
    Code:
     x=(1,2,3,5,6)
    x=x[:3]+(4,)+x[3:]
    x
    (1, 2, 3, 4, 5, 6)

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to insert value into tuple?

    You have to make new tuple to insert new value. After this you can re connect the name from the old tuple to the new one. In this case you have to use += operator to do this. You can do this in following ways.
    Code:
    thestups += ('1000.00',)
    After this you have to write following code.
    Code:
    bs=list(mystuple)
     bs.append("main file")
     as=tuple(bs)

Similar Threads

  1. Can I insert SSD into Eee PC?
    By OPinaArTy in forum Portable Devices
    Replies: 5
    Last Post: 08-02-2011, 03:20 PM
  2. How to use Listings and Tuple in Python?
    By Dante i in forum Software Development
    Replies: 4
    Last Post: 04-01-2011, 03:50 AM
  3. Use of the insert() in C++
    By Gavyn in forum Software Development
    Replies: 5
    Last Post: 12-03-2010, 03:56 PM
  4. What is tuple variables in SQL?
    By Shophia_D in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 12:51 PM
  5. SQL problem if tuple is already in the database
    By Macbeth in forum Software Development
    Replies: 4
    Last Post: 17-11-2009, 09:02 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,950,840.27443 seconds with 16 queries