tags:

views:

68

answers:

4

Accessing tuple values

How to access the value of a and b in the following

       >> t=[]
       >> t.append(("a" , 1))
       >> t.append(("b" , 2))
       >> print t[0][0]
        a
       >> print t[1][0] 
         b

How to print the values of a and b

+5  A: 

Just do

print t[0][1]
print t[1][1]

But of course if you really want to look up for a or b, this is not the best construct, then you need a dict:

t = {}
t["a"] = 1
t["b"] = 2
print t["a"]
print t["b"]
KillianDS
A: 
print t[0][1]
print t[1][1]

??

Plumenator
+2  A: 

Its simple than expected,

>> t=[]
>> t.append(("a" , 1))
>> t.append(("b" , 2))
>> dict(t).get('a') 
# 1

Happy Coding.

simplyharsh
:)...................
Hulk
+1  A: 
>>> t
[('a', 1), ('b', 2)]
>>> t[0][1]
1
>>> t[1][1]
2
shiki