returning the 2nd value of a sublist.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • v13tn1g
    New Member
    • Feb 2009
    • 31

    returning the 2nd value of a sublist.

    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(as df, L)------------> it would return 4.0

    how can i do this? any help would be appreciated.
  • Smygis
    New Member
    • Jun 2007
    • 126

    #2
    Why not use a dictionary instead? That is exactly what you are trying to do.
    Code:
    >>> D = dict([ ["asdf",4.0], ["fdsa",8], ["abcd",98]])
    >>> D
    {'abcd': 98, 'fdsa': 8, 'asdf': 4.0}
    >>> D["asdf"]
    4.0
    >>>

    Comment

    • v13tn1g
      New Member
      • Feb 2009
      • 31

      #3
      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

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        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.

        Comment

        • kaarthikeyapreyan
          New Member
          • Apr 2007
          • 106

          #5
          algorith coded

          boxfish algorithm coded :).
          Originally posted by boxfish
          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.
          Code:
          >>> l=[["asdf",4.0],["fdsa",8],["abcd",98]]
          >>> def return_value(key,list_):
          ...   for entity in list_:
          ...     if entity[0] == key:
          ...       return entity[1]
          ... 
          >>> return_value('asdf',l)
          4.0
          >>>

          Comment

          Working...