By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

returning the 2nd value of a sublist.

P: 31
lets say L=[ [asdf,4.0], [fdsa,8], [abcd,98]

how can i get the 2nd value of the sublist returned. for example if it were a function

return_value(asdf, L)------------> it would return 4.0

how can i do this? any help would be appreciated.
Feb 8 '09 #1
Share this Question
Share on Google+
4 Replies


Smygis
100+
P: 126
Why not use a dictionary instead? That is exactly what you are trying to do.
Expand|Select|Wrap|Line Numbers
  1. >>> D = dict([ ["asdf",4.0], ["fdsa",8], ["abcd",98]])
  2. >>> D
  3. {'abcd': 98, 'fdsa': 8, 'asdf': 4.0}
  4. >>> D["asdf"]
  5. 4.0
  6. >>> 
  7.  
  8.  
Feb 8 '09 #2

P: 31
i dont think i can use dictionaries yet because our class is not there yet, i think i have to base my answers using methods of lists.

thanks for the reply though
Feb 8 '09 #3

boxfish
Expert 100+
P: 469
Try to make a for loop that looks at each of the sub-lists, and if the first element of the sub-list is equal to the given key, return the second element.
I hope this is helpful.
Feb 8 '09 #4

kaarthikeyapreyan
100+
P: 107
boxfish algorithm coded :).
@boxfish
Expand|Select|Wrap|Line Numbers
  1. >>> l=[["asdf",4.0],["fdsa",8],["abcd",98]]
  2. >>> def return_value(key,list_):
  3. ...   for entity in list_:
  4. ...     if entity[0] == key:
  5. ...       return entity[1]
  6. ... 
  7. >>> return_value('asdf',l)
  8. 4.0
  9. >>> 
  10.  
Feb 9 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.