[Zope] stripping values from string

Mark McEahern marklists@mceahern.com
Wed, 8 Jan 2003 10:29:26 -0600


> I have two list from my input form:
>
> ListA: [a,b,c,d]
> ListB: [1,2,3,4]
>
> How can i strip ListA and ListB so that the
> first value of ListA will correspond to the first value of ListB
> and the second of A to sencond of B and so on:
>
> Example: (a,1), (b,2),(c,3),(d,4)

In Python, you'd just use zip:

$ python
Python 2.2.1 (#1, Jun 25 2002, 10:55:46)
[GCC 2.95.3-5 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = ['a', 'b', 'c', 'd']
>>> b = [1, 2, 3, 4]
>>> zip(a, b)
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

I assume you're doing this in a Python script?  Try zip.  I haven't tried it
and I don't know whether zip is considered restricted (not sure why it would
be); I'm just saying I haven't tried this from a Python Script inside Zope
(although I don't see why it wouldn't work).

You could also just iterate over one of the lists and append a tuple of its
element and the corresponding element from the other list into a new list.

assert len(a) == len(b)
new_list = []
for i in range(len(a)):
  new_list.append((a[i], b[i]))

// m

-