Skip to content

Instantly share code, notes, and snippets.

@niwinz
Created July 4, 2012 22:43
Show Gist options
  • Save niwinz/3049916 to your computer and use it in GitHub Desktop.
Save niwinz/3049916 to your computer and use it in GitHub Desktop.
kata: make list of list as plain list of elements
#from functools import reduce # uncomment if python3+ is used
lmb = lambda x, y: x+reduce(lmb, y, []) if isinstance(y, list) else x + [y]
reduce(lmb,[1,['a','b'],2,[[1,2, [3]]],'c'], [])
import functools
m = [1, ['a', 'b'], 2, [ [ 1, 2, [3]]], 'c']
def myreduce(x, y):
if isinstance(y, list): return x + functools.reduce(myreduce, y, [])
else: return x+[y]
functools.reduce(myreduce, m, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment