471,356 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,356 software developers and data experts.

return dictionary values as single list

50
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
Aug 15 '07 #1
6 1630
ilikepython
844 Expert 512MB
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.
Aug 15 '07 #2
bartonc
6,596 Expert 4TB
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. >>> 
Aug 15 '07 #3
kdt
50
Sweet, thanks. Much better!
Aug 15 '07 #4
bartonc
6,596 Expert 4TB
Sweet, thanks. Much better!
You did see mine, right? One minute apart, maybe you were posting while I was.
Aug 15 '07 #5
ilikepython
844 Expert 512MB
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.  
Aug 15 '07 #6
bartonc
6,596 Expert 4TB
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.
Aug 15 '07 #7

Post your reply

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

Similar topics

47 posts views Thread by Martin DeMello | last post: by
125 posts views Thread by Raymond Hettinger | last post: by
90 posts views Thread by Christoph Zwerschke | last post: by
7 posts views Thread by Andrew Robinson | last post: by
4 posts views Thread by =?utf-8?B?TWFjaWVqIEJsaXppxYRza2k=?= | last post: by
14 posts views Thread by lee | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.