473,516 Members | 3,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!

Dec 15 '05 #1
7 1214
On 15 Dec 2005 09:19:37 -0800 in comp.lang.python, "Tuvas"
<tu*****@gmail.com> 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.
Dec 15 '05 #2

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 '05 #3
Il 2005-12-15, Tuvas <tu*****@gmail.com> 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"
Dec 15 '05 #4
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!

Dec 15 '05 #5
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
Dec 15 '05 #6
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 'o****@xiludom.gro'.split('@')])"
Dec 15 '05 #7
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)
Dec 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
7018
by: Ivan Voras | last post by:
Are there any performance/size differences between using tuples and using lists? -- -- Every sufficiently advanced magic is indistinguishable from technology - Arthur C Anticlarke
42
2684
by: Jeff Wagner | last post by:
I've spent most of the day playing around with lists and tuples to get a really good grasp on what you can do with them. I am still left with a question and that is, when should you choose a list or a tuple? I understand that a tuple is immutable and a list is mutable but there has to be more to it than just that. Everything I tried with a...
3
2106
by: Thorsten Kampe | last post by:
I found out that I am rarely using tuples and almost always lists because of the more flexible usability of lists (methods, etc.) To my knowledge, the only fundamental difference between tuples and lists is that tuples are immutable, so if this is correct, than list are a superset of tuples, meaning lists can do everything tuples can do and...
66
4906
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
11
16772
by: Noah | last post by:
I have a list of tuples I want to reverse the order of the elements inside the tuples. I know I could do this long-form: q = y = for i in y: t=list(t)
5
2348
by: fff_afafaf | last post by:
Do you know is it possible to put different kinds of tuples to one container? E.g. to a vector? (The lengths of the tuples are different, and also the types in the tuples are different.. -Is it possible to make a pointer, which can point to all of these tuples?) #include <tr1/tuple>
10
5133
by: rshepard | last post by:
While working with lists of tuples is probably very common, none of my five Python books or a Google search tell me how to refer to specific items in each tuple. I find references to sorting a list of tuples, but not extracting tuples based on their content. In my case, I have a list of 9 tuples. Each tuple has 30 items. The first two items...
12
4141
by: rshepard | last post by:
I'm a bit embarrassed to have to ask for help on this, but I'm not finding the solution in the docs I have here. Data are assembled for writing to a database table. A representative tuple looks like this: ('eco', "(u'Roads',)", 0.073969887301348305) Pysqlite doesn't like the format of the middle term: pysqlite2.dbapi2.InterfaceError:...
122
5412
by: C.L. | last post by:
I was looking for a function or method that would return the index to the first matching element in a list. Coming from a C++ STL background, I thought it might be called "find". My first stop was the Sequence Types page of the Library Reference (http://docs.python.org/lib/typesseq.html); it wasn't there. A search of the Library Reference's...
27
1652
by: seberino | last post by:
Please help me think of an example where immutable tuples are essential. It seems that everywhere a tuple is used one could just as easily use a list instead. chris
0
7273
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7574
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3265
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3252
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.