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

return dictionary values as single list

P: 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
Share this Question
Share on Google+
6 Replies


ilikepython
Expert 100+
P: 844
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
Expert 5K+
P: 6,596
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

P: 50
kdt
Sweet, thanks. Much better!
Aug 15 '07 #4

bartonc
Expert 5K+
P: 6,596
Sweet, thanks. Much better!
You did see mine, right? One minute apart, maybe you were posting while I was.
Aug 15 '07 #5

ilikepython
Expert 100+
P: 844
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
Expert 5K+
P: 6,596
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.