# Tuples

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

1. ### TuvasGuest

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

2. ### Lawrence OluyedeGuest

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

3. ### Dave HansenGuest

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
4. ### 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
5. ### Xavier MorelGuest

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!
>

>>> 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
6. ### TuvasGuest

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
7. ### Larry BatesGuest

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.

saying "Why won't this work?".

Were you trying something like this on tuples? That would not
work because tuples are immutable.

Larry Bates

Larry Bates, Dec 15, 2005
8. ### bruno at modulixGuest

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