printing dictionary and tuple

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Mar 19 18:32:13 EDT 2008


En Wed, 19 Mar 2008 08:16:52 -0300, Beema shafreen  
<beema.shafreen at gmail.com> escribió:

> i am trying to print the dictionary values and tuple in a same line as  
> below
>
> print "\t".join(dict[a].values())+'\t'+"\t".join(b)
>
> Error I get is the  TypeError,
>  since i have  misisng values in the dictionary. if i use exception i  
> will
> miss those
> how should i print the data without missing the lines excluding the error
> separated by tab.

What is a? What is b? Their contents? I *guess* this is what you want:

a = {'some': 'values',
      3.14: 'in a',
      1234: 'dictionary'}
b = ('99', 'bottles', 'of', 'beer')
print '\t'.join(a.values()) + '\t' + '\t'.join(b)

The code above only works if all values are strings - else you could get a  
TypeError. In that case, convert all items to string before joining:

a = {'some': 'values',
      'in a': 3.14,
      'dictionary': 1234}
b = (99, 'bottles', 'of', 'beer')
print ('\t'.join([str(value) for value in a.values()])
        + '\t'
        + '\t'.join([str(item) for item in b]))

If this is not your problem, please provide a complete example next time,  
and the full exception traceback is very important too.

-- 
Gabriel Genellina




More information about the Python-list mailing list