Welcome, guest | Sign In | My Account | Store | Cart

lace: forms a list of tuples by interlacing elements from several lists.

Python, 21 lines
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21``` ```def lace(*lists): """lace: forms a list of tuples by interlacing elements from several lists. That is i-th tuple contains the i-th element from each of the argument sequences. e.g.: >>> a=[1,2,3,4]; b=[100,101,102,103]; c=['w','x','y','z'] >>> d=lace(a,b,c); print d; # yields a single list of nested tuples: [(1, 100, 'w'), (2, 101, 'x'), (3, 102, 'y'), (4, 103, 'z')] * NOTE: Unlike the zip built-in (new to Python 2.0), Short lists are extended with values of None to match the longest list if necessary. With zip(), the returned list is truncated in length to the length of the shortest argument sequence which is not always kosher. """ return apply(map,(None,)+lists) x = [1, 2, 3, 4, 5, 6, 7, 8, 9] y = [200, 202, 204, 206, 208, 210, 212, 214, 216, 218, 220, 222, 224, 226, 228] z = ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] foo = lace(x, y, z); foo # try this and see! ```

works like the new built in zip function with one key difference: short lists get padded with None rather than having the long lists get truncated. Obviously this is the oldest python trick and nothing one could copyright or anything, but still a useful recipe, especially for those who are horrified to see that zip eats the list elements of lists that run long. Hope to get the shortest recipe in the book.

 Created by kevin parks on Fri, 15 Mar 2002 (PSF)

### Tags

• (none)
▶ Show machine tags (4)

### Required Modules

• (none specified)