How to Convert a Python Dictionary to List

Table of Contents

 >>> from functools import reduce
 >>> a = {'foo': 'bar', 'baz': 'quux', 'hello': 'world'}
 >>> list(reduce(lambda x, y: x + y, a.items()))
 ['foo', 'bar', 'baz', 'quux', 'hello', 'world']

explanation
-> a.items() returns a list of tuples. 
-> Adding two tuples together makes one tuple containing all elements. Thus the reduction creates one tuple containing all keys and values and then the list(…) makes a list from that.











Related posts