Tuples

T

Tuvas

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

Lawrence Oluyede

Il 2005-12-15, Tuvas <[email protected]> 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]]
 
D

Dave Hansen

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
 
S

skip

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
 
X

Xavier Morel

Tuvas said:
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)
 
T

Tuvas

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

Larry Bates

Tuvas said:
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
 
B

bruno at modulix

Tuvas said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top