return dictionary values as single list | Member | | Join Date: Mar 2007
Posts: 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. -
>>> 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
|  | Expert | | Join Date: Feb 2007
Posts: 839
| | | 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. -
>>> 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 -
s = [value for tup in d['a'] for value in tup]
-
Hope that helps.
|  | Moderator | | Join Date: Sep 2006 Location: Minden, Nevada, USA
Posts: 6,400
| | | 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. -
>>> 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 -
>>> aList = [(45, 6), (56, 4)]
-
>>> newList = []
-
>>> for tup in aList:
-
... newList.extend(tup)
-
...
-
>>> newList
-
[45, 6, 56, 4]
-
>>>
| | Member | | Join Date: Mar 2007
Posts: 50
| | | re: return dictionary values as single list
Sweet, thanks. Much better!
|  | Moderator | | Join Date: Sep 2006 Location: Minden, Nevada, USA
Posts: 6,400
| | | 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.
|  | Expert | | Join Date: Feb 2007
Posts: 839
| | | re: return dictionary values as single list Quote:
Originally Posted by bartonc -
>>> aList = [(45, 6), (56, 4)]
-
>>> newList = []
-
>>> for tup in aList:
-
... newList.extend(tup)
-
...
-
>>> newList
-
[45, 6, 56, 4]
-
>>>
Just for fun, you could do that with map: -
>>> aList = [(45, 6), (56, 4)]
-
>>> newList = []
-
>>> map(newList.extend, aList)
-
>>> newList
-
[45, 6, 56, 4]
-
|  | Moderator | | Join Date: Sep 2006 Location: Minden, Nevada, USA
Posts: 6,400
| | | re: return dictionary values as single list Quote:
Originally Posted by ilikepython Just for fun, you could do that with map: -
>>> aList = [(45, 6), (56, 4)]
-
>>> newList = []
-
>>> map(newList.extend, aList)
-
>>> newList
-
[45, 6, 56, 4]
-
Bonus points to you, my friend! Two lines in one; very nicely done.
|  | |