Connecting Tech Pros Worldwide Forums | Help | Site Map

return dictionary values as single list

kdt kdt is offline
Member
 
Join Date: Mar 2007
Posts: 50
#1: Aug 15 '07
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.

Expand|Select|Wrap|Line Numbers
  1. >>> d['a']= [(45,6)]
  2. >>> d['a'].append((56,4))
  3. >>> d['a']
  4. [(45, 6), (56, 4)]
  5. >>> s =[]
  6. >>> for i, j in enumerate(d['a']):
  7. ...     for k, l in enumerate(j):
  8. ...         s.append(l)
  9. ...         
  10. >>> s
  11. [45, 6, 56, 4]
  12. >>> 
  13.  
thanks



ilikepython's Avatar
Expert
 
Join Date: Feb 2007
Posts: 839
#2: Aug 15 '07

re: return dictionary values as single list


Quote:

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.

Expand|Select|Wrap|Line Numbers
  1. >>> d['a']= [(45,6)]
  2. >>> d['a'].append((56,4))
  3. >>> d['a']
  4. [(45, 6), (56, 4)]
  5. >>> s =[]
  6. >>> for i, j in enumerate(d['a']):
  7. ...     for k, l in enumerate(j):
  8. ...         s.append(l)
  9. ...         
  10. >>> s
  11. [45, 6, 56, 4]
  12. >>> 
  13.  
thanks

Expand|Select|Wrap|Line Numbers
  1. s = [value for tup in d['a'] for value in tup]
  2.  
Hope that helps.
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400
#3: Aug 15 '07

re: return dictionary values as single list


Quote:

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.

Expand|Select|Wrap|Line Numbers
  1. >>> d['a']= [(45,6)]
  2. >>> d['a'].append((56,4))
  3. >>> d['a']
  4. [(45, 6), (56, 4)]
  5. >>> s =[]
  6. >>> for i, j in enumerate(d['a']):
  7. ...     for k, l in enumerate(j):
  8. ...         s.append(l)
  9. ...         
  10. >>> s
  11. [45, 6, 56, 4]
  12. >>> 
  13.  
thanks

Expand|Select|Wrap|Line Numbers
  1. >>> aList = [(45, 6), (56, 4)]
  2. >>> newList = []
  3. >>> for tup in aList:
  4. ...     newList.extend(tup)
  5. ...     
  6. >>> newList
  7. [45, 6, 56, 4]
  8. >>> 
kdt kdt is offline
Member
 
Join Date: Mar 2007
Posts: 50
#4: Aug 15 '07

re: return dictionary values as single list


Sweet, thanks. Much better!
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400
#5: Aug 15 '07

re: return dictionary values as single list


Quote:

Originally Posted by kdt

Sweet, thanks. Much better!

You did see mine, right? One minute apart, maybe you were posting while I was.
ilikepython's Avatar
Expert
 
Join Date: Feb 2007
Posts: 839
#6: Aug 15 '07

re: return dictionary values as single list


Quote:

Originally Posted by bartonc

Expand|Select|Wrap|Line Numbers
  1. >>> aList = [(45, 6), (56, 4)]
  2. >>> newList = []
  3. >>> for tup in aList:
  4. ...     newList.extend(tup)
  5. ...     
  6. >>> newList
  7. [45, 6, 56, 4]
  8. >>> 

Just for fun, you could do that with map:
Expand|Select|Wrap|Line Numbers
  1. >>> aList = [(45, 6), (56, 4)]
  2. >>> newList = []
  3. >>> map(newList.extend, aList)
  4. >>> newList
  5. [45, 6, 56, 4]
  6.  
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400
#7: Aug 15 '07

re: return dictionary values as single list


Quote:

Originally Posted by ilikepython

Just for fun, you could do that with map:

Expand|Select|Wrap|Line Numbers
  1. >>> aList = [(45, 6), (56, 4)]
  2. >>> newList = []
  3. >>> map(newList.extend, aList)
  4. >>> newList
  5. [45, 6, 56, 4]
  6.  

Bonus points to you, my friend! Two lines in one; very nicely done.
Reply