Hide
Meet people who work on similar things as you – get help if you need it Join Siafoo Now or Learn More

Recursively Reduce a Tuple, List, or Dict Atom Feed 0

In Brief Wraps python's reduce() function to work recursively, and on tuples, lists, or strings.
# 's
 1# Copyright 2007 Regents of the University of California
2# Written by David Isaacson at the University of California, Davis
3# BSD License
4
5def Reduce(function,args):
6 """
7 Perfoms a Pythonic 'reduce()' on tuples, lists, or dicts. Recursive.
8
9 Objects can contain other objects. Reduction will occur recursively.
10 """
11 if args is None: return None
12 if type(args) == tuple: args = list(args)
13 if type(args) == dict: args = list(args.values())
14 for i in xrange(len(args)):
15 if type(args[i]) == tuple or type(args[i]) == list: args[i] = Reduce(function,args[i])
16 elif type(args[i]) == dict:
17 args[i] = Reduce(function,args[i].values())
18 return reduce(function,args)

Wraps python's reduce() function to work recursively, and on tuples, lists, or strings.