Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 71 Online
OpenStudy (anonymous):

I have a list of tuples. Each tuple has two values. I need to be able to return the value of the 0'th (is that a word? hah) index of the tuple that resides in the 0th index of the list. I can easily pull the tuple in which I want to work, from the list, but I can't seem to figure out how to-- in a single step-- pull the first element of a tuple from the first tuple... if anyone can help, I'd greatly appreciate it. Just let me know if my question was too convoluted or incoherent- Thanks!

OpenStudy (anonymous):

you could do this value_a, value_b = tuple_list[0]

OpenStudy (e.mccormick):

The tuple is indexed. >>> tup=(3,'a',9,'hi') >>> print tup (3, 'a', 9, 'hi') >>> val=tup[0] >>> print val 3 >>>

OpenStudy (e.mccormick):

Here is another cute one for you: >>> lis=[(1,2),(3,4),(5,6)] # a list of tuples >>> print lis [(1, 2), (3, 4), (5, 6)] >>> print lis[1] # one element from the list (3, 4) >>> print lis[1][0] # only part of a tuple inside the list! 3 >>>

OpenStudy (anonymous):

In Python 3.x: tupList = [(7, 9), (2, 1), (4, 6), (9, 2)] print(tupList[0][0]) Output: 7 For Python 2.x, use: print tupList[0][0]

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!