return dictionary values as single list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kdt
    New Member
    • Mar 2007
    • 50

    return dictionary values as single list

    Can anyone suggest a better way of returning the values in a dictionary as a single list. I have the following, but it uses a nested loop, not sure if there is a more efficient way.

    Code:
    >>> d['a']= [(45,6)]
    >>> d['a'].append((56,4))
    >>> d['a']
    [(45, 6), (56, 4)]
    >>> s =[]
    >>> for i, j in enumerate(d['a']):
    ... 	for k, l in enumerate(j):
    ... 		s.append(l)
    ... 		
    >>> s
    [45, 6, 56, 4]
    >>>
    thanks
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by kdt
    Can anyone suggest a better way of returning the values in a dictionary as a single list. I have the following, but it uses a nested loop, not sure if there is a more efficient way.

    Code:
    >>> d['a']= [(45,6)]
    >>> d['a'].append((56,4))
    >>> d['a']
    [(45, 6), (56, 4)]
    >>> s =[]
    >>> for i, j in enumerate(d['a']):
    ... 	for k, l in enumerate(j):
    ... 		s.append(l)
    ... 		
    >>> s
    [45, 6, 56, 4]
    >>>
    thanks
    [code=python]
    s = [value for tup in d['a'] for value in tup]
    [/code]
    Hope that helps.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by kdt
      Can anyone suggest a better way of returning the values in a dictionary as a single list. I have the following, but it uses a nested loop, not sure if there is a more efficient way.

      Code:
      >>> d['a']= [(45,6)]
      >>> d['a'].append((56,4))
      >>> d['a']
      [(45, 6), (56, 4)]
      >>> s =[]
      >>> for i, j in enumerate(d['a']):
      ... 	for k, l in enumerate(j):
      ... 		s.append(l)
      ... 		
      >>> s
      [45, 6, 56, 4]
      >>>
      thanks
      [CODE=python]
      >>> aList = [(45, 6), (56, 4)]
      >>> newList = []
      >>> for tup in aList:
      ... newList.extend( tup)
      ...
      >>> newList
      [45, 6, 56, 4]
      >>> [/CODE]

      Comment

      • kdt
        New Member
        • Mar 2007
        • 50

        #4
        Sweet, thanks. Much better!

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by kdt
          Sweet, thanks. Much better!
          You did see mine, right? One minute apart, maybe you were posting while I was.

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by bartonc
            [CODE=python]
            >>> aList = [(45, 6), (56, 4)]
            >>> newList = []
            >>> for tup in aList:
            ... newList.extend( tup)
            ...
            >>> newList
            [45, 6, 56, 4]
            >>> [/CODE]
            Just for fun, you could do that with map:
            [code=python]
            >>> aList = [(45, 6), (56, 4)]
            >>> newList = []
            >>> map(newList.ext end, aList)
            >>> newList
            [45, 6, 56, 4]
            [/code]

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by ilikepython
              Just for fun, you could do that with map:
              [code=python]
              >>> aList = [(45, 6), (56, 4)]
              >>> newList = []
              >>> map(newList.ext end, aList)
              >>> newList
              [45, 6, 56, 4]
              [/code]
              Bonus points to you, my friend! Two lines in one; very nicely done.

              Comment

              Working...