tags:

views:

78

answers:

3

And have a big blob of values, with a space in between?

Edit: What if I have nested dictionaries? The current solutions work if my values are all strings. But what if my values are nested dictionaries?

+2  A: 

Assuming the values are already strings:

>>> d = { 1 : 'foo', 2 : 'bar' }
>>> ' '.join(d.values())
'foo bar'

If not, you can try to convert them to strings using for example str:

>>> d = { 1 : 2, 3: 4 }
>>> ' '.join(str(v) for v in d.values())
'2 4'
Mark Byers
What if my dictionary is nested?
TIMEX
+1  A: 
>>> a = {1: 'hello', 2: 'world'}
>>> a.values()
['hello', 'world']
>>> ' '.join(a.values())
'hello world'
Thomas
And a.keys() for list of keys. There is also iterkeys() and itervalues() I guess to get iterators. To get pair of key-value iteritems() (or items() for list variant)
ony
@ony: Note that iterkeys, itervalues and iteritems won't work in Python 3.
Mark Byers
A: 

if you want the values from a dictionary that contains dictionaries as values, try something like this:

In [1]: from itertools import chain
In [2]: d = {'A': {1: 'pants', 2: 'trowsers'}, 'B': {'1': 'spam', '2': 'eggs'}} 
In [3]: values = ' '.join(chain.from_iterable(dic.itervalues() for dic in d.itervalues()))
In [4]: values
Out[4]: 'pants trowsers spam eggs'
Autoplectic