merging lists

Greg Jorgensen gregj at pobox.com
Tue Mar 21 04:53:26 EST 2000


"55555" <55555 at dakotacom.net> wrote in message
news:38d73758_1 at news5.newsfeeds.com...
> I'm trying to lay out two lists of strings next to each other with a tab
> in between.  For example:
>
> incList = ['asdf', 'zxcv']
> decList = ['sdfg', 'dgfh', 'wert']
>
> a = map(lambda x,y: x+'\t'+y, incList, decList)
>
> I get a type error mentioning something about bad operand types.  Is there
> either an easier way to do this, or could someone explain what I'm doing
> wrong?


The problem is the lists aren't the same length. map will get None values
from the shorter list(s), but the + operator can't concatenate a string with
None.

If you can't be sure the lists are the same length, you can do something
like this:

def join(s,t):
    if s == None:
        s = ''
    if t == None:
        t = ''
    return str(s) + '\t' + str(t)

...
a = map(join, incList, decList)


Greg Jorgensen
gregj at pobox.com






More information about the Python-list mailing list