Sakura
Sakura

Reputation: 729

Concatenate strings at the same indexes in two lists

I have two lists and I'd like to combine them following the same order.

Below is the question.

A = ['1,2,3','4,5,6','7,8,9']
B = ['10','11','12']

To get a new list such as below

A+B = ['1,2,3,10','4,5,6,11','7,8,9,12']

I try extend, zip, append, enumerate but could not get what I want. Two loops the result will repeat.

Any hint or elegant way to do this please?

Upvotes: 5

Views: 3564

Answers (8)

Andrew Winterbotham
Andrew Winterbotham

Reputation: 1010

How about this, assuming the two lists are of the same length:

def concat_lists(l1, l2):
    concat_list = []
    for i in range(len(l1)):
        concat_list.append(l1[i] + ',' + l2[i])
    return concat_list

Or using a list comprehension instead:

def concat_lists(l1, l2):
    return [l1[i] + ',' + l2[i] for i in range(len(l1))]

Upvotes: 2

Padraic Cunningham
Padraic Cunningham

Reputation: 180411

You can also map str.join after zipping:

A = ['1,2,3','4,5,6','7,8,9']
B = ['10','11','12']

from itertools import izip

print(map(",".join, izip(A, B)))
['1,2,3,10', '4,5,6,11', '7,8,9,12']

Upvotes: 2

Matiu Carr
Matiu Carr

Reputation: 298

Already answered, so here are some fun and games -should work if A and B are different lengths -zip leaves out unmatched stuff:

>>> A = ['1,2,3','4,5,6','7,8,9']
>>> B = ['10','11','12']

# basic solution using for/len, will except if len(A) > len(B)
>>> [ A[i] + "," + B[i] for i in range(len(A)) ]

# complicated solution to deal with a difference in the
# lengths of A and B 
>>> [ (A[i] if i < len(A) else ',,') + "," + (B[i] if i < len(B) else '') for i in range((len(A) if len(A)>=len(B) else len(B))) ]
['1,2,3,10', '4,5,6,11', '7,8,9,12']

# add something to A, len(A) > len(B)
>>> A.append('13,14,15')
>>> [ (A[i] if i < len(A) else ',,') + "," + (B[i] if i < len(B) else '') for i in range((len(A) if len(A)>=len(B) else len(B))) ]
['1,2,3,10', '4,5,6,11', '7,8,9,12', '13,14,15,']

# add a couple of things to B, len(B) > len(A)
>>> B.append('16')
>>> B.append('17')
>>> [ (A[i] if i < len(A) else ',,') + "," + (B[i] if i < len(B) else '') for i in range((len(A) if len(A)>=len(B) else len(B))) ]
['1,2,3,10', '4,5,6,11', '7,8,9,12', '13,14,15,16', ',,,17']

Upvotes: 2

gsb-eng
gsb-eng

Reputation: 1209

By using map ...certainly this method won't create any extra list of tuples like zip..

>>> A = ['1,2,3','4,5,6','7,8,9']
>>> B = ['10','11','12']
>>> map(lambda x, y:x + ',' + y, A, B)
['1,2,3,10', '4,5,6,11', '7,8,9,12']

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304167

You can certainly use enumerate, although zip is the more natural choice

>>> A = ['1,2,3','4,5,6','7,8,9']
>>> B = ['10','11','12']
>>> [a + "," + B[i] for i, a in enumerate(A)]
['1,2,3,10', '4,5,6,11', '7,8,9,12']

Upvotes: 2

Mike M&#252;ller
Mike M&#252;ller

Reputation: 85442

[a + ',' + b for a, b in zip(A, B)]

Upvotes: 4

Jakube
Jakube

Reputation: 3565

A and B are lists of strings. Using zip, you can create pairs like ('1,2,3', '10'). Afterwards you can combine these two strings using join.

A = ['1,2,3','4,5,6','7,8,9']
B = ['10','11','12']

C = [','.join(z) for z in zip(A, B)]
print C

Upvotes: 6

Chad S.
Chad S.

Reputation: 6633

Just use ','.join and zip..

A = ['1,2,3','4,5,6','7,8,9']
B = ['10','11','12']

C = [ ','.join(pair) for pair in zip(A,B) ]

Upvotes: 5

Related Questions