[Tutor] Accessing a tuple element

Andrei Kulakov ak@silmarill.org
Fri, 16 Nov 2001 23:45:18 -0500


On Fri, Nov 16, 2001 at 09:32:05PM -0700, Mike Yuen wrote:
> I have a question regarding accessing a tuple within a dictionary.
> I've got a whole bunch of points on a matrix inside a dictionary as
> follows:
> dict {(1, 2): 4, (8, 11): 2, etc etc}
> My question is, I would like to access the first and occasionally the
> second element of the tuples.
> 
> I know I can access a tuple element simply by indexing into it but it
> doesn't seem to work if a tuple is a dictionary key.
> Does anyone know if there's a way to do this?

The key should be atomic.. how about {4: (1,2)}? The whole idea of
having a key is being able to say "what data is associated with this
key?".

But if you *really* need this, you can do:

for key in dict.keys():
 elem1 = key[0]
 [do stuff with elem1]


> 
> Thanks,
> M
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org