Tuples

Discussion in 'Python' started by Tuvas, Dec 15, 2005.

  1. Tuvas

    Tuvas Guest

    Let's say I make a program something like follows:

    x=[]
    x.append([1,2,3])
    x.append([4,5,6])
    print x
    print x[0]
    print x[0][1]
    x[0][1]=5

    Okay, everything works here as expected except the last line. Why won't
    this work? Thanks for the help!
    Tuvas, Dec 15, 2005
    #1
    1. Advertising

  2. Il 2005-12-15, Tuvas <> ha scritto:

    First, these are lists not tuples

    > Let's say I make a program something like follows:
    >
    > x=[]
    > x.append([1,2,3])
    > x.append([4,5,6])
    > print x
    > print x[0]
    > print x[0][1]
    > x[0][1]=5
    >
    > Okay, everything works here as expected except the last line. Why won't
    > this work? Thanks for the help!
    >


    Why not? for me works. After the last sentence if you print out x you get:

    [[1, 5, 3], [4, 5, 6]]


    --
    Lawrence - http://www.oluyede.org/blog
    "Anyone can freely use whatever he wants but the light at the end
    of the tunnel for most of his problems is Python"
    Lawrence Oluyede, Dec 15, 2005
    #2
    1. Advertising

  3. Tuvas

    Dave Hansen Guest

    On 15 Dec 2005 09:19:37 -0800 in comp.lang.python, "Tuvas"
    <> wrote:

    >Let's say I make a program something like follows:
    >
    >x=[]
    >x.append([1,2,3])
    >x.append([4,5,6])
    >print x
    >print x[0]
    >print x[0][1]
    >x[0][1]=5
    >
    >Okay, everything works here as expected except the last line. Why won't
    >this work? Thanks for the help!


    Perhaps because you're not expecting the right thing? Here's what I
    get:

    >>> x = []
    >>> x.append([1,2,3])
    >>> x.append([4,5,6])
    >>> print x, x[0], x[0][1]

    [[1, 2, 3], [4, 5, 6]] [1, 2, 3] 2
    >>> x[0][1]=5
    >>> print x

    [[1, 5, 3], [4, 5, 6]]
    >>>


    Which is what i would expect. Regards,

    -=Dave

    --
    Change is inevitable, progress is not.
    Dave Hansen, Dec 15, 2005
    #3
  4. Tuvas

    Guest

    Tuvas> Let's say I make a program something like follows:
    Tuvas> x=[]
    Tuvas> x.append([1,2,3])
    Tuvas> x.append([4,5,6])
    Tuvas> print x
    Tuvas> print x[0]
    Tuvas> print x[0][1]
    Tuvas> x[0][1]=5

    Tuvas> Okay, everything works here as expected except the last line. Why
    Tuvas> won't this work?

    You didn't say what you expected to happen, but it works just fine for me:

    >>> x = []
    >>> x.append([1,2,3])
    >>> x.append([4,5,6])
    >>> print x

    [[1, 2, 3], [4, 5, 6]]
    >>> print x[0]

    [1, 2, 3]
    >>> print x[0][1]

    2
    >>> x[0][1] = 5
    >>> print x

    [[1, 5, 3], [4, 5, 6]]

    Skip
    , Dec 15, 2005
    #4
  5. Tuvas

    Xavier Morel Guest

    Tuvas wrote:
    > Let's say I make a program something like follows:
    >
    > x=[]
    > x.append([1,2,3])
    > x.append([4,5,6])
    > print x
    > print x[0]
    > print x[0][1]
    > x[0][1]=5
    >
    > Okay, everything works here as expected except the last line. Why won't
    > this work? Thanks for the help!
    >

    Works for me, do you have more informations?

    >>> x = list()
    >>> x.append([1,2,3])
    >>> x.append([4,5,6])
    >>> print x

    [[1, 2, 3], [4, 5, 6]]
    >>> print x[0]

    [1, 2, 3]
    >>> print x[0][1]

    2
    >>> x[0][1] = 5
    >>> print x[0][1]

    5
    >>>


    Now if you're really using tuples, that last line won't work because
    tuples are immutable e.g. you can't modify a tuple's elements after the
    tuple's creation

    >>> y = list()
    >>> y.append((1,2,3))
    >>> y.append((4,5,6))
    >>> print y

    [(1, 2, 3), (4, 5, 6)]
    >>> print y[0]

    (1, 2, 3)
    >>> print y[0][1]

    2
    >>> y[0][1] = 5


    Traceback (most recent call last):
    File "<pyshell#16>", line 1, in -toplevel-
    y[0][1] = 5
    TypeError: object does not support item assignment
    >>>


    And the reason is explicitly stated (tuples don't support item assignment)
    Xavier Morel, Dec 15, 2005
    #5
  6. Tuvas

    Tuvas Guest

    Never mind, I just realized that my code was actually a list inside of
    a tuple, and not a tuple inside of a tuple, thus giving the confusion
    that it had. Thanks for the help!
    Tuvas, Dec 15, 2005
    #6
  7. Tuvas

    Larry Bates Guest

    Tuvas wrote:
    > Let's say I make a program something like follows:
    >
    > x=[]
    > x.append([1,2,3])
    > x.append([4,5,6])
    > print x
    > print x[0]
    > print x[0][1]
    > x[0][1]=5
    >
    > Okay, everything works here as expected except the last line. Why won't
    > this work? Thanks for the help!
    >

    Works just fine for me.

    Suggestion: You should ALWAYS post your traceback instead of just
    saying "Why won't this work?".

    Your subject says "Tuples" then your example is on lists.
    Were you trying something like this on tuples? That would not
    work because tuples are immutable.

    Larry Bates
    Larry Bates, Dec 15, 2005
    #7
  8. Tuvas wrote:
    > Let's say I make a program something like follows:
    >
    > x=[]
    > x.append([1,2,3])
    > x.append([4,5,6])
    > print x
    > print x[0]
    > print x[0][1]
    > x[0][1]=5


    Where are the tuples ?

    > Okay, everything works here as expected except the last line. Why won't
    > this work?


    Because you forgot to pay the salary ?-)

    "don't work" is probably the worst description of a problem. Please take
    time to explain what you expect and what you actually get.


    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
    bruno at modulix, Dec 15, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Kish
    Replies:
    11
    Views:
    797
    Jeff Kish
    Oct 21, 2004
  2. Replies:
    5
    Views:
    541
    Thomas J. Gritzan
    Oct 6, 2006
  3. tuples within tuples

    , Oct 26, 2007, in forum: Python
    Replies:
    12
    Views:
    551
    Dennis Lee Bieber
    Oct 27, 2007
  4. xera121
    Replies:
    8
    Views:
    706
    lolmc
    Sep 30, 2009
  5. Jon Reyes
    Replies:
    18
    Views:
    219
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page