Skip to content

Instantly share code, notes, and snippets.

@Pk13055
Created June 17, 2017 13:27
Show Gist options
  • Save Pk13055/d6e316ed463088b7a5c0b04ad55b344e to your computer and use it in GitHub Desktop.
Save Pk13055/d6e316ed463088b7a5c0b04ad55b344e to your computer and use it in GitHub Desktop.
List manipulator scripts
# this function recursively performs an operation on nested lists element-wise
import operator
def list_recur(l1, l2, op = operator.add):
if not l1:
return type(l1)([])
elif isinstance(l1[0], type(l1)):
return type(l1)([list_recur(l1[0], l2[0], op)]) + list_recur(l1[1:],l2[1:], op)
else:
return type(l1)([op(l1[0], l2[0])]) + list_recur(l1[1:], l2[1:], op)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment