Velocity Reviews > Tuples

# Tuples

Tuvas
Guest
Posts: n/a

 12-15-2005
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!

Lawrence Oluyede
Guest
Posts: n/a

 12-15-2005
Il 2005-12-15, Tuvas <(E-Mail Removed)> 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"

Dave Hansen
Guest
Posts: n/a

 12-15-2005
On 15 Dec 2005 09:19:37 -0800 in comp.lang.python, "Tuvas"
<(E-Mail Removed)> 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.

skip@pobox.com
Guest
Posts: n/a

 12-15-2005

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

Xavier Morel
Guest
Posts: n/a

 12-15-2005
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)

Tuvas
Guest
Posts: n/a

 12-15-2005
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!

Larry Bates
Guest
Posts: n/a

 12-15-2005
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

bruno at modulix
Guest
Posts: n/a

 12-15-2005
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 '(E-Mail Removed)'.split('@')])"