开发者

Is there a shorter code to join the keys and values for a dictionary?

开发者 https://www.devze.com 2023-04-09 05:42 出处:网络
Is there a shorter or better snippet that accomplishes the following: >>> h = { \'apple\' : \'ipad\' , \'amazon\': \'kindle\' }

Is there a shorter or better snippet that accomplishes the following:

>>> h = { 'apple' : 'ipad' , 'amazon': 'kindle' }
>>开发者_开发问答> [' '.join(item) for item in zip( h.keys(), h.values())]
[ 'apple ipad', 'amazon kindle' ]


>>> [' '.join(item) for item in h.iteritems()]
['amazon kindle', 'apple ipad']

>>> map(' '.join, h.iteritems())
['amazon kindle', 'apple ipad']

But avoid using map() these days. And list comprehensions if you can help it.

0

精彩评论

暂无评论...
验证码 换一张
取 消